- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我试图找到在 PHP 中调用魔法方法的顺序。因此写了一个非常基础的程序
class testme
{
public $var1;
/*function __construct()
{
echo'<br/> Constructor called';
}*/
public function __set($name, $value)
{
echo'<br/> You are in sssset function';
}
public function __call($method,$arg)
{
echo '<br/> call method';
}
public function __get($name)
{
echo'<br/> You are in get function';
}
public function __isset($name)
{
echo'<br/> You are in isset function';
}
public function __unset($name)
{
echo'<br/> You are in unset function';
}
function __destruct() {
print "<br/>Destroying " . $this->name . "\n";
}
}
$obj = new testme;
$obj->var1=5;
预期的输出是
You are in set function
Destroying
获得:
You are in get function
Destroying
$obj->var1=5
在这里,我将值设置为类 var 然后它调用 __get
的原因。这里有什么问题?
最佳答案
如果您在 __get
中对 $name
执行 var_dump
,您会看到它包含 name
. __get
函数实际上是在__destruct
中调用的。这是因为 $var1
是可访问成员,因此不会调用 __get
或 __set
函数。
The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms "inaccessible properties" and "inaccessible methods" to refer to this combination of declaration and visibility.
由于 $var1
已定义且公开,因此不会调用魔法方法。
关于php - __get() 函数在 php 中的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778526/
我在模拟重载的 __get($index) 方法时遇到问题。被模拟的类和使用它的被测系统的代码如下: vars[$index])) { return $this->vars[$
我想实现这样的效果: class Foo { public $bar; // array('aaa' => array('bbb' => 'ccc')) public function _
我有一个类,我正在使用 __set。因为我不希望它设置任何东西,所以我有一组已批准的变量,它会在实际设置类属性之前检查这些变量。 但是,在构建时,我希望 __construct 方法设置几个类属性,其
使用这样的示例类: class Test{ public function &__get($name){ print_r($name); } } Test 的一个实例将
我通常会尝试远离 PHP 的魔术方法,因为它们似乎混淆了对象的公共(public)接口(interface)。也就是说,它们似乎被越来越多地使用,至少在我读过的代码中,所以我不得不问:对于何时使用它们
我遇到的情况并不复杂,但我花了一些时间才弄明白。 class A { private $data = array('key' => 'value'); public function
我通过利用 __get 来定义当访问不存在的属性时会发生什么神奇的方法。 所以如果 $property->bla不存在我会得到null . return (isset($this->$name)) ?
class Dog { protected $bark = 'woof!'; public function __get($key) { if (isset($this
想知道是否可以执行以下操作(我知道代码不会按预期工作,只是试图达到目的): class Form { private $v = array(); function __set($var
Other similar questions: Why I should use __get() and __set()-magic methods in php? When do/should I
Matt Zandstra 在他的课本“PHP 对象模式与实践”中给出了以下示例来说明 __get() 方法: class Person { function __get( $property
我看过如下代码,很奇怪__get方法被调用了两次,为什么? class Foo { private $bar; function __get($name){ echo
使用 PHP 5.3 在将 empty() 应用于通过 __get() 重载函数获取的动态对象属性时,我遇到了奇怪/不直观的行为。考虑以下代码片段: 23, 'name'=> 'my stri
我正在使用 PHP 实现 OOP 设计。我想知道 PHP 如何处理其神奇方法(如 __get 和 __set)的继承。 class Foo { protected $property1;
自从我的技术书籍告诉我如此以来,我想知道这是不是真的,例如: class A { public $attr = "hmm"; } $a = new A(); echo $a->attr; //
我的函数导致错误“WC_Abstract_Legacy_Order-> __ get,wc_doing_it_wrong»。我已经了解了相同的问题,但仍然找不到错误的调用位置,请提供帮助。 Wooco
我正在研究神奇的 PHP 方法并想出了非常奇怪的行为。我的代码: "A", "b" => "B", "c" => "C"]; protected $c = [1, 2, 3]; pu
这个问题在这里已经有了答案: PHP Inherited parent method can't access child's private property (3 个答案) 关闭 9 年前。 $
我试图找到在 PHP 中调用魔法方法的顺序。因此写了一个非常基础的程序 class testme { public $var1; /*function __construct()
我有一个定制的 MVC PHP 框架,我正在重写它并且对性能和魔术方法有疑问。对于框架的模型部分,我在想如果 __get/__set 魔法方法会导致太多的性能损失而不值得使用。我的意思是访问(读取和写
我是一名优秀的程序员,十分优秀!