doufu7835 2011-03-02 21:42
浏览 85
已采纳

在方法中调用构造函数

Somehow my mind went far away from the current problem and I made a terrible mistake.. I called a parent constructor inside a method that just initializes classes properties.. Or did I.. The parent constructor's job was to set the ID value. Well PHP allowed me to do that. But isnt that just wrong? And it looks like I can call classes own constructor in the same way.. Isn't it that constructors should only be allowed to call when creating instances of a class... And they are only called when creating instances..

<?php

    class A {

        public function __construct() {
            echo "Test<br />";
        }

    }

    class B extends A {

        public function test() {
            parent::__construct();
        }

    }

    $b = new B();
    $b->test();

    // OUTPUT:
    // Test
    // Test

?>

EDIT: So the conclusion is that PHP allows you to call the constructor inside a method but it actually does nothing. And that other "TEST" string comes from the base class's constructor.

  • 写回答

4条回答 默认 最新

  • douhuan7862 2011-03-02 22:05
    关注

    One word describes the situation exactly.

    Override

    What happens is that when the parent has a method such as a __construct and the child class does not have that method, when the method is called on the child class it calls the parents method.

    For example:

    class Parent
    {
         public function start(){}
    }
    class Child extends Parent
    {
    
    }
    

    if I call the the start method on the Child class its executes the Parent::Start(), but if Child class has a method called Start() that one would be called as its Overriding the parents version.

    Every class has a constructor method, and php executes that to compile the class into an object, now if you have a method called __construct() in a class, PHP Calls the internal construct which compiles the class, and then calls your override constructor.

    If you do not have a __construct method in the Child class PHP executes the parent __construct

    This is the reason why you get 2 x Test in your Object initialization.

    The only time you should be using parent:__cosntruct() is if your Child class requires a user defined constructor, you explicitly call the parent construct to prepare the parent object.

    For example:

    class Parent
    {
         public function __construct()
         {
             //Do work on Parent Class
         }
    }
    
    class Child extends Parent
    {
        public function __construct()
        {
             //Do Work on Child Class
    
             parent::__construct();
        }
    }
    

    The reason you should only ever call a parent constructor within a child constructor and not a child method is that constructors should only ever be called once, this way enforces that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划