Jump to content

Difference between __get and __set?


eldan88
Go to solution Solved by eldan88,

Recommended Posts

Hey guys. I am little bit confused on when to use __set() and when to use __get().

 

When using __set(), I am basically setting a property when creating an object. I still can get the value of the property when calling it. 

With that being said, what would be and  ideal way of using the __get() magic method?

 

Just to show you an example, if i set name postal_code to 1111 I can just call that postal code with using the __get()

<?php 
class Classexample {
	

function __set($name,$value) {
	$this->$name = $value;

	}
	
     }
$class = new Classexample;
$class->postal_code = 11111;
echo $class->postal_code;


?>
Edited by eldan88
Link to comment
Share on other sites

Most of the time you use __get and __set to put the data in a different place. If you're just setting it on the object like normal then there's no point using either because that's the default behavior anyways. Since the data is in a different place, if you implement one method you'll very likely need to implement the other.

 

The exception would be using __get and __set with inaccessible members, like

class Example {
	public $one;
	protected $two;
	private $three;

	public function __get($name) { return $this->$name; }
	public function __set($name, $value) { $this->$name = $value; }

	public function test() {
		// none of these will trigger __get or __set
		$this->one = 123; echo $this->one;
		$this->two = 234; echo $this->two;
		$this->three = 345; echo $this->three;
	}

}

class Child extends Example {
	public function test2() {
		// will not trigger __get/__set
		$this->one = 123; echo $this->one;
		$this->two = 234; echo $this->two;
		// will because it's not accessible
		$this->three = 345; echo $this->three;
	}
}

$e = new Example();
// will not trigger __get/__set
$this->one = 123; echo $this->one;
// will because it's not accessible
$this->two = 234; echo $this->two;
$this->three = 345; echo $this->three;
As for "ideal", that depends why you want to use them.
Link to comment
Share on other sites

As an example of "..put[ting] the data in a different place", take a basic template class for example:

class Template {
    protected $mVars=array();
   
    public function __get($nm){
        return isset($this->mVars[$nm])?$this->mVars[$nm]:null;
    }

    public function __set($nm,$v){
        $this->mVars[$nm] = $v;
    }

    public function display($file){
        extract($this->mVars);
        include($file);
    }
}
$tpl = new Template();
$tpl->Name = 'kicken';
$tpl->Date = date('Y-m-d');
$tpl->display('somefile.tpl');
The class stores all the variables to be used in the template inside the $mVars property. However, by using __get/__set you allow a user to get/set the variables as if they were real properties on the object. By shoving all the variables inside an array rather than directly on the object you prevent the possibility of someone accidentally overwriting some other property that the class might depend on.
Link to comment
Share on other sites

Most of the time you use __get and __set to put the data in a different place. If you're just setting it on the object like normal then there's no point using either because that's the default behavior anyways. Since the data is in a different place, if you implement one method you'll very likely need to implement the other.

 

The exception would be using __get and __set with inaccessible members, like

class Example {
	public $one;
	protected $two;
	private $three;

	public function __get($name) { return $this->$name; }
	public function __set($name, $value) { $this->$name = $value; }

	public function test() {
		// none of these will trigger __get or __set
		$this->one = 123; echo $this->one;
		$this->two = 234; echo $this->two;
		$this->three = 345; echo $this->three;
	}

}

class Child extends Example {
	public function test2() {
		// will not trigger __get/__set
		$this->one = 123; echo $this->one;
		$this->two = 234; echo $this->two;
		// will because it's not accessible
		$this->three = 345; echo $this->three;
	}
}

$e = new Example();
// will not trigger __get/__set
$this->one = 123; echo $this->one;
// will because it's not accessible
$this->two = 234; echo $this->two;
$this->three = 345; echo $this->three;
As for "ideal", that depends why you want to use them.

 

 

Okay. I got you! I see what you have done. You have use __set() to set the values and __get() to get them. In my last example I used __set() to to set and get the values.

 

But thanks. You examples makes a lot of sense!!!! :happy-04:

Link to comment
Share on other sites

  • Solution

As an example of "..put[ting] the data in a different place", take a basic template class for example:

class Template {
    protected $mVars=array();
   
    public function __get($nm){
        return isset($this->mVars[$nm])?$this->mVars[$nm]:null;
    }

    public function __set($nm,$v){
        $this->mVars[$nm] = $v;
    }

    public function display($file){
        extract($this->mVars);
        include($file);
    }
}
$tpl = new Template();
$tpl->Name = 'kicken';
$tpl->Date = date('Y-m-d');
$tpl->display('somefile.tpl');
The class stores all the variables to be used in the template inside the $mVars property. However, by using __get/__set you allow a user to get/set the variables as if they were real properties on the object. By shoving all the variables inside an array rather than directly on the object you prevent the possibility of someone accidentally overwriting some other property that the class might depend on.

 

Gotchya! Makes a lot of sense now!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.