#44370 [Bgs]: Magic methods __set, __get, __isset, __unset are triggered if a member exists

From: Date: Sun, 09 Mar 2008 00:49:33 +0000
Subject: #44370 [Bgs]: Magic methods __set, __get, __isset, __unset are triggered if a member exists
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-123092@lists.php.net to get a copy of this message
 ID:               44370
 User updated by:  aldo at armiento dot com
 Reported By:      aldo at armiento dot com
 Status:           Bogus
 Bug Type:         Class/Object related
 Operating System: Linux 2.6.18
 PHP Version:      5.2.5
 New Comment:

Hello Felipe,
thanks for your time and accept my apologies.

So if you need to 'unset' a class member without destroy the member 
class reference it you have to use $test->test = null instead of 
unset($test->test).

I hoped that _a class member would be always remained such_ also after

an 'unset' call.

Thanks again,
Aldo


Previous Comments:
------------------------------------------------------------------------

[2008-03-08 15:47:20] felipe@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

This is expected, you are destroing the property.

class foo {
	public $test;
	public function __construct() {
		$this->test = 1;
	}	
	public function __set($a, $b) {
		print "set: $a\n";
	}
	public function __get($a) {
		print "get: $a\n";
	}
}

$test = new foo;
print $test->test;
unset($test->test); // destroy!
print "\n";
$test->test = 'new property';
print $test->test;

------
1
set: test
get: test


------------------------------------------------------------------------

[2008-03-08 10:36:33] aldo at armiento dot com

Description:
------------
Magic methods __set, __get, __isset, __unset are triggered also if a 
member exists if accessing member from a method used in a cURL callback

function.

From the documentation instead "These methods will only be triggered 
when your object or inherited object doesn't contain the member or 
method you're trying to access".

Reproduce code:
---------------
<?php

class Test
{
	public $member;
	
	public function __set($sName, $mValue)
	{
		echo "__set() called\n";
	}
	
	public function __get($sName)
	{
		echo "__get() called\n";
	}
	
	public function __isset($sName)
	{
		echo "__isset() called\n";
	}
	
	public function __unset($sName)
	{
		echo "__unset() called\n";
	}
	
	public function headerCallBack($hCurl, $sHeader)
	{
		$this->member = 'value';
		strtolower($this->member);
		isset($this->member);
		unset($this->member);
		
		return strlen($sHeader);
	}
	
	public function httpGet()
	{
		$hCurl = curl_init();
		curl_setopt($hCurl, CURLOPT_URL, 'http://www.php.net/');
		curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($hCurl, CURLOPT_HEADERFUNCTION, array($this,
'headerCallBack'));
		curl_exec($hCurl);
	}
}

$C = new Test();
$C->httpGet();

Expected result:
----------------
Nothing.

Actual result:
--------------
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called
__set() called
__get() called
__isset() called
__unset() called


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=44370&edit=1



Thread (3 messages)

« previous php.bugs (#123092) next »