Last active
January 23, 2020 18:18
-
-
Save akash-coded/b18b5752265e69caf9585a8ee602256a to your computer and use it in GitHub Desktop.
Some Python Magic Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal { | |
| } | |
| class Penguin extends Animal { | |
| public function __construct($id) { | |
| $this->getPenguinFromDb($id); | |
| } | |
| public function getPenguinFromDb($id) { | |
| // elegant and robust database code goes here | |
| } | |
| public function __get($field) { | |
| if($field == 'name') { | |
| return $this->username; | |
| } | |
| } | |
| public function __set($field, $value) { | |
| if($field == 'name') { | |
| $this->username = $value; | |
| } | |
| } | |
| public function __call($method, $args) { | |
| echo "unknown method " . $method; | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| We looked at an example of using the clone keyword in the second part of my introduction to OOP in PHP, to make a copy of an object rather than have two variables pointing to the same actual data. By overriding this method in a class, we can affect what happens when the clone keyword is used on this object. While this isn't something we come across every day, a nice use case is to create a true singleton by adding a private access modifier to the method. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal{ | |
| public function __construct() { | |
| $this->created = time(); | |
| $this->logfile_handle = fopen('/tmp/log.txt', 'w'); | |
| } | |
| } | |
| class Penguin extends Animal { | |
| } | |
| $tux = new Penguin; | |
| echo $tux->created; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Animal{ | |
| public function __construct() { | |
| $this->created = time(); | |
| $this->logfile_handle = fopen('/tmp/log.txt', 'w'); | |
| } | |
| public function __destruct() { | |
| fclose($this->logfile_handle); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Penguin extends Animal { | |
| public function __construct($id) { | |
| $this->getPenguinFromDb($id); | |
| } | |
| public function getPenguinFromDb($id) { | |
| // elegant and robust database code goes here | |
| } | |
| } | |
| $tux = new Penguin(3); | |
| echo $tux->name . " is " . $tux->age . " years old\n"; | |
| class Penguin extends Animal { | |
| public function __construct($id) { | |
| $this->getPenguinFromDb($id); | |
| } | |
| public function getPenguinFromDb($id) { | |
| // elegant and robust database code goes here | |
| } | |
| public function __get($field) { | |
| if($field == 'name') { | |
| return $this->username; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Penguin extends Animal { | |
| public function __construct($id) { | |
| $this->getPenguinFromDb($id); | |
| } | |
| public function getPenguinFromDb($id) { | |
| // elegant and robust database code goes here | |
| } | |
| public function __get($field) { | |
| if($field == 'name') { | |
| return $this->username; | |
| } | |
| } | |
| public function __set($field, $value) { | |
| if($field == 'name') { | |
| $this->username = $value; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The __sleep() method is called when the object is serialised, and allows you to control what gets serialised. There are all sorts of applications for this, a good example is if an object contains some kind of pointer, for example a file handle or a reference to another object. When the object is serialised and then unserialised then these types of references are useless since the target may no longer be present or valid. Therefore it is better to unset these before you store them. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Penguin { | |
| public function __construct($name) { | |
| $this->species = 'Penguin'; | |
| $this->name = $name; | |
| } | |
| public function __toString() { | |
| return $this->name . " (" . $this->species . ")\n"; | |
| } | |
| } | |
| $tux = new Penguin('tux'); | |
| echo $tux; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| This is the opposite of the __sleep() method and allows you to alter the behaviour of the unserialisation of the object. Used in tandem with __sleep(), this can be used to reinstate handles and object references which were removed when the object was serialised. A good example application could be a database handle which gets unset when the item is serialised, and then reinstated by referring to the current configuration settings when the item is unserialised. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment