php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35252 __get not called if parameter exists
Submitted: 2005-11-17 04:55 UTC Modified: 2005-11-17 08:24 UTC
From: php at seven dot net dot nz Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5CVS-2005-11-17 (CVS) OS: Win XP
Private report: No CVE-ID: None
 [2005-11-17 04:55 UTC] php at seven dot net dot nz
Description:
------------
When overloading a class, the __get magic function is not called if the parameter in question has been set on the object. I'm not sure if this is by design, but it seems to defeat the purpose of it somewhat.

Reproduce code:
---------------
<?php
class Cat {

	public function __get ($variable) {

		if (method_exists ($this, "get_$variable"))
			return $this->{"get_$variable"} ();
		elseif (isset ($this->$variable))
			return $this->$variable;
		else
			throw new Exception ("$variable not found");

	}

	public function get_noise () {
		return "Meow!";
	}

}

$test = new Cat;
print $test->noise."\n";
$test->noise = "Woof!";
print $test->noise."\n";
print $test->get_noise ()."\n";
print $test->__get ("noise")."\n";
?>

Expected result:
----------------
Meow!
Meow!
Meow!
Meow!

Actual result:
--------------
Meow!
Woof!
Meow!
Meow!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-17 08:24 UTC] derick@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 how it should work. __get() and __set() only work on non-set properties.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 17 17:01:29 2024 UTC