Jump to content


Photo

Question about using __get()


  • Please log in to reply
2 replies to this topic

#1 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationNew York

Posted 25 August 2013 - 01:37 PM

Hey,

 

 I have been learning about magic methods, and I am little bit confused about how the __get() methods works below.

 

I don't understand the code that goes below the comment "//Attempt to return a protected property by name"

 

How does it know what property name to return??

function __get($name) {
	//Postal code look up if unset
	if(!$this->_postal_code)
	$this->_postal_code = $this->_postal_code_guess();
	
	
	//Attempt to return a protected property by name
	$protected_property_name =  "-" . $name;
	if(property_exists($this, $protected_property_name)) {
	return $this->$protected_property_name;	
	}
	
	//Unable to access property trigger error
	trigger_error('Undefined property via __get:', $name);
	return NULL;
	
}

 
protected function _postal_code_guess() {
return 'LOOKUP';	
}


#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,708 posts
  • LocationBonita, FL

Posted 25 August 2013 - 02:09 PM

It generates the name by concatenating '-' with $name. $name will be the name of the property which was trying to be accessed. For example:
$obj=new MagicObject;
echo $obj->SomeProperty;
If there is no SomeProperty variable defined in class MagicObject (or it is inaccessible due to visibility rules), php will try to access it by calling __get('SomeProperty'), as if that code were re-written to:
$obj=new MagicObject;
echo $obj->__get('SomeProperty');
So in the __get code from your post, what happens is $protected_property_name gets set to the value '-'.$name; resulting in -SomeProperty.

Then it checks if that variable exists on the object and if so, returns it.
Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 eldan88

eldan88

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationNew York

Posted 29 August 2013 - 12:09 AM

It generates the name by concatenating '-' with $name. $name will be the name of the property which was trying to be accessed. For example:

$obj=new MagicObject;
echo $obj->SomeProperty;
If there is no SomeProperty variable defined in class MagicObject (or it is inaccessible due to visibility rules), php will try to access it by calling __get('SomeProperty'), as if that code were re-written to:
$obj=new MagicObject;
echo $obj->__get('SomeProperty');
So in the __get code from your post, what happens is $protected_property_name gets set to the value '-'.$name; resulting in -SomeProperty.

Then it checks if that variable exists on the object and if so, returns it.

 

Oh okay! I got it now... I am getting used to how magic methods works. Thanks for the clear explination!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com