相关疑难解决方法(0)

魔术方法(__get,__ set)在扩展类中不起作用?

<?php 

abstract class AbstractClass
{
    public function __get($theName)
    {
        return (isset($this->$theName)) ? $this->$theName : NULL;
    }

    public function __set($theName, $theValue)
    {
        if (false === property_exists(get_class(), $theName)) {
            throw new Exception(get_class()." does not have '".$theName."' property.");
        } else {
            $this->$theName = $theValue;
        }
    }
}

class ConcreteClass extends AbstractClass
{
        private $x;
        private $y;

        public function __construct($theX, $theY)
        {
            $this->x = $theX;
            $this->y = $theY;
        }
}

$concreteClass = new ConcreteClass(10, 20);

var_dump( $concreteClass->x );
Run Code Online (Sandbox Code Playgroud)

有没有办法让这项工作或我必须将这些魔术方法添加到扩展类?

php

4
推荐指数
2
解决办法
8563
查看次数

标签 统计

php ×1