gpt4 book ai didi

php - 魔术方法(__get、__set)在扩展类中不起作用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:57:04 25 4
gpt4 key购买 nike

<分区>

<?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 );

有什么方法可以完成这项工作,还是我必须将这些魔术方法添加到扩展类中?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com