dongyi3616 2011-07-30 22:11
浏览 41
已采纳

从子方法访问时,父方法属性设置的父类属性为空

I'm having trouble understanding why I can access a property from my parent class, but it's NULL, even though it has already been set by the parent (and has not been knowingly reset). I thought it might be because that property was set by a private method, but no difference when I changed to public. Here's a radically simplified example:

class TheParent
{

    protected $_parent_property;

    function __construct()
    {}

    private function parent_method($property);
    {
        $this->_parent_property = $property;
            $call = new TheChild;
            $call->child_method();
    }
}

class TheChild extends TheParent
{ 
    function __construct()
    {
        parent::construct();
    }

    public function child_method();
    {
        echo $this->_parent_property;
            exit;
    }
}

$test = new TheParent;
$test->parent_method('test');

I worked around this by passing the parent property to the child when the child is constructed by the parent ie new TheChild($this->_parent_property), but I still don't understand why $this->_parent_property is set to NULL when accessed from the child in my original example.

I do know that if I set this property from the parent constructor, I'd be able to access it just fine. I'm trying to understand why a property set by a parent method, and accessible by other parent methods, is not accessible from the child class which extends the parent.

Can anyone explain? Thanks!

  • 写回答

2条回答 默认 最新

  • duan1396 2011-07-30 22:15
    关注

    The problem is that you're creating a new instance where the variable isn't set. The property is bound to a particular instance, so you're creating one instance of the parent and then from the parent another instance of the child,i which includes all the stuff creating a new parent would contain, including $_parent_property. When you read the value in the child, you're reading the value of a newly created parent, not the one you previously created.

    In effect, you do this:

    A = new TheParent()
    A->_parent_property = 'test'
    

    Calls: B = new TheChild() underneath the covers, this does new TheParent()

    Print B->_parent_property (which was uninitialized)

    Consider this similar example that will produce your expected result:

    class TheParent
    {
    
        protected $_parent_property;
    
        function __construct()
        {
            parent_method();
        }
    
        private function parent_method();
        {
            $this->_parent_property = 'test';
        }
    }
    
    class TheChild extends TheParent
    { 
        function __construct()
        {
            parent::construct();
        }
    
        public function child_method();
        {
            echo $this->_parent_property;
            exit;
        }
    }
    
    $child = new TheChild();
    $child->child_method();
    

    In this example, the private method in TheParent is invoked on the same instance created by TheChild, setting the underlying instance variable.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度