Jump to content

HELP WITH UNDERSTANDING THE MAGIC FUNCTIONS __get() and __set()


chams

Recommended Posts

I stumbled upon this book where i first  met __set(). When I research it, I met __get(). So there, I don't know who they are. And what they exactly do. If I  may ask, is there any tutorial which can give me proper introduction with __set() and __get()? I have read for beginners, but I dont know, maybe I'm too dummy to understand.  :( please bare with me i am just  beginner. Thanks in advance.

Link to comment
Share on other sites

They are magic...They are used in object's to "automatically" add properties(class variables) on the fly. So if you don't need to define the variables in your class ahead of time if you define the magic method. The magic method is where you would define how your store your "dynamic variable"(for lack of better word).

 

You would be able to access the property as if it were a public property of the class...

 

Its more of an OOP topic....some of the OOP tutorials on PHPFreaks make mention of it but dont use it...

Link to comment
Share on other sites

__set() is used to assign class variables that don't exist. Likewise, __get() is used to return class variables that don't exist. You can see some examples on the php manual.

 

http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

 

And to see all magic methods: http://www.php.net/manual/en/language.oop5.magic.php

Link to comment
Share on other sites

You can add properties dynamically to any object:

 

class MyObject {}
$obj = new MyObject();
$obj->propertyName = 'propertyValue';
print_r(get_object_vars($obj)); // Array ( [propertyName] => propertyValue )

 

This works even for classes that do not implement __set() and __get().

 

If you want to disable/overwrite this behavior then implement __set:

 

class MyObject {
    public function __set($k, $v) {}
}
$obj = new MyObject(); // object
$obj->propertyName = $propertyValue;
print_r(get_object_vars($obj)); // Array ( )

 

This does not work if the property actually exists (that is: with a public access modifier)

 

class MyObject {
    public $propertyName = '';
    public function __set($k, $v) {}
}
$obj = new MyObject(); // object
$obj->propertyName = 'propertyValue';
print_r(get_object_vars($obj)); // Array ( [propertyName] => propertyValue )

 

In Conclusion

 

__set() and __get() allow you to create, read, and write to dynamic properties. A dynamic property is a property that has not been defined upon construction of the object (public $propertyName = '';.

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.