php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65870 Wrong documentation about magic methods __get and __isset
Submitted: 2013-10-09 14:19 UTC Modified: 2016-06-25 16:45 UTC
From: v dot matsuk at tut dot by Assigned: cmb (profile)
Status: Not a bug Package: Documentation problem
PHP Version: 5.4.20 OS: Windows 7 x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: v dot matsuk at tut dot by
New email:
PHP Version: OS:

 

 [2013-10-09 14:19 UTC] v dot matsuk at tut dot by
Description:
------------
http://www.php.net/manual/en/language.oop5.overloading.php states:
>Note:
The return value of __set() is ignored because of the way PHP processes the assignment operator. Similarly, __get() is never called when chaining assignments together like this:
 $a = $obj->b = 8;

But it's wrong for __get()!  __get() is called when chaining assignments together like this: $a = $obj->b = 8.

The Russian version of documentation (http://www.php.net/manual/ru/language.oop5.overloading.php) states:
> Замечание:
Невозможно использовать перегруженные свойства в других языковых конструкциях, кроме isset(). Это означает, что если на перегруженном свойстве будет вызвана конструкция empty(), то перегруженный метод не будет вызван.
Для обхода этого ограничения можно скопировать перегруженное свойство в локальную переменную и затем применить к ней empty().

English version doesn't contain this text. It's also wrong because if method __isset was realised, the empty() will call it if property not found.

Test script:
---------------
class A {
   protected $_vars = array();

   public function __get($var){return $this->_vars[$var];}
   public function __set($var, $val){$this->_vars[$var] = $val; }
   public function __isset($var){return isset($this->_vars[$var]);}
}

$o = new A();

$o->a = 1;
$o->b = 2;

$c = $o->b = 3;
var_dump($c); //3
var_dump($o->b); //3

var_dump(empty($o->b)); //false

Expected result:
----------------
null
int (3)
true (if i beleive the russian version of documentation)


Actual result:
--------------
int (3)
int (3)
false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-25 16:45 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-25 16:45 UTC] cmb@php.net
> __get() is called when chaining assignments together like this:
> $a = $obj->b = 8.

No, see <https://3v4l.org/Gq9PP>.

> English version doesn't contain this text.

If translations deviate from the original, report that as bug of
type translation problem, or consider to submit a patch via
<https://edit.php.net/>.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Oct 26 16:01:23 2020 UTC