douhao1956 2014-02-20 02:44
浏览 44
已采纳

PHP get_class_methods和get_class_vars:为什么我需要这个? [关闭]

I been studying PHP and I discovered these two functions get_class_methods and get_class_vars. I understand how they work, but it is not clear how they can be useful to me. I mean I can always look at the class and get by myself this kind of information. Can you give a real example?

I really appreciate any help.

  • 写回答

1条回答 默认 最新

  • dongpan8928 2014-02-20 04:48
    关注

    I am not sure if you will be able to fully understand why and how this is working at this point. You will need some experience with OOP to get to the point where you realize and understand why you may need to use this.

    Reflection

    As you probably already know from the php documentation, the existence of get_class_methods and get_class_vars allows you to retrieve the class's methods and variables. Although you can just open the class and see the methods and properties for yourself, sometimes you might need to do it programmatically. Furthermore, you might want to change certain properties or methods and alter the behavior at runtime. This technique is referred as Reflection (Wiki , PHP).

    When would I need to use Reflection?

    As @mario mentioned in his comment:

    1. Developing modular platforms
    2. Developing certain complex tools
    3. Routing
    4. Reverse-engineering code
    5. Probably many other use cases I can't think of right now.

    Examples?

    You might find it easier to understand how things work if you have a look at the PHPUnit framework. It is a tool which provides the developers the ability to test their code via test cases. The test cases are usually stored in a class and every method tests a certain functionality. Here is an example test case class:

    class UserTest extends \PHPUnit_Framework_TestCase  {
    
        public function testUserCreate(){
            $firstName = "FirstName";
            $lastName  = "LastName";
    
            $user = new User();
            $user->setFirstName($firstName);
            $user->setLastName($lastName);
    
            assertEquals("FirstName", $user->getFirstName(), "Setting first name failed! Expected ".$firstName." but got ".$user->getFirstName()."!");
            assertEquals("LastName", $user->getLastName(), "Setting last name failed! Expected ".$lastName." but got ".$user->getLastName()."!");
        }
    
        public function testUpdateUser(){ ... }
        public function testDeleteUser(){ ... }
    
    }
    

    When PHPUnit is called and it starts executing the class above, all it will know about it would be the name of the class. PHPUnit will need to know the names of all public methods in that class so it could execute them. Therefore it will use the reflection technique to retrieve the methods of this class and then begin executing them one by one.

    I hope this clarifies things.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题