简体   繁体   English

PHP 4:无法理解 OOP 行为

[英]PHP 4: Can not understand the OOP behaviour

While practicing for certification, I came across one MCQ the question.在练习认证时,我遇到了一个 MCQ 这个问题。 It was as below.如下所示。

Q: Consider the following script.问:考虑以下脚本。 What will it output?它会输出什么?

<?php
$global_obj = null;
class my_class
{
   var $value;
   function my_class()
   {
      global $global_obj;
      $global_obj = &$this;
   }
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
?>

Choose Right One Options:选择正确的一个选项:

  • A. 5 A. 5
  • B. 10 B. 10
  • C. Nothing C. 没什么
  • D. The constructor will throw an error D. 构造函数会抛出错误
  • E. 510 E. 510

I chose and B: 10 as because in my_class constructor $global_obj is being initialized by the reference of $this .我选择和 B: 10 因为在my_class构造函数中$global_obj正在由$this的引用初始化。 By when I cross checked my answer was wrong当我交叉检查时,我的答案是错误的

As a description they mentioned that作为描述,他们提到

This is a really tricky one.这是一个非常棘手的问题。 Upon first examination, it would seem that the constructor of my_class stores a reference to itself inside the $global_obj variable .第一次检查时,似乎my_class的构造函数在$global_obj variable存储了对自身的引用。 Therefore, one would expect that, when we later change the value of $global_obj->my_value to 10, the corresponding value in $a would change as well.因此,可以预期,当我们稍后将$global_obj->my_value的值更改为 10 时,$a 中的相应值也会更改。 Unfortunately, the new operator does not return a reference, but a copy of the newly created object.不幸的是,new 运算符不返回引用,而是返回新创建对象的副本。 Therefore, the script will output 5 and the correct answer is A.因此,脚本将输出 5,正确答案是 A。


Ya I agree the description is well enough but still I am not able to digest it as because we have clearly assigned $global_obj the reference of $this then how can be this possible?是的,我同意描述已经足够好,但我仍然无法消化它,因为我们已经明确地$global_obj分配了$global_obj的引用,那么这怎么可能呢? Can any one please explain in detail?哪位大神能详细解释一下吗?

we have clearly assigned $global_obj the reference of $this then how can be this possible?我们已经明确地为 $global_obj 分配了 $this 的引用,那么这怎么可能呢?

You assign the global variable within the constructor.构造函数中分配全局变量。 At that time, you are referencing kind of a temporary object.那时,您正在引用某种临时对象。 Then the constructor returns a copy of that temporary object.然后构造函数返回该临时对象的副本 This is that copy that $a will reference (whereas the global var still references the temporary object).这是 $a 将引用的副本(而全局变量仍然引用临时对象)。

Going OOP with php 4 is nuts.使用 php 4 进行 OOP 是很疯狂的。 I guess you should not understand the OOP behaviour of PHP 4 but just accept it...我想你不应该理解 PHP 4 的 OOP 行为,而只是接受它......

this script will output "5" which would be A along with the following warning Warning: Creating default object from empty value此脚本将输出“5”,这将是 A 以及以下警告警告:从空值创建默认对象

the reason for this is when you set this原因是当你设置这个

                 `$global_obj = &$this;`

what your assigning is reference to the current object您的分配是对当前对象的引用

this article may help这篇文章可能会有所帮助

http://www.webmaster-source.com/2010/02/25/why-do-some-php-variables-have-an-ampersand-before-them/ http://www.webmaster-source.com/2010/02/25/why-do-some-php-variables-have-an-ampersand-before-them/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM