php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44370 Magic methods __set, __get, __isset, __unset are triggered if a member exists
Submitted: 2008-03-08 10:36 UTC Modified: 2008-03-09 00:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: aldo at armiento dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.5 OS: Linux 2.6.18
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aldo at armiento dot com
New email:
PHP Version: OS:

 

 [2008-03-08 10:36 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-03-08 15:47 UTC] 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-09 00:49 UTC] aldo at armiento dot com
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Dec 16 21:00:02 2025 UTC