Created
September 14, 2016 12:18
-
-
Save kirilkirkov/ad4e12d0149c6ebaa42b74fd78bd6b08 to your computer and use it in GitHub Desktop.
PHP Magic methods - Getters and Setters
This file contains 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
<?php | |
class MyClass { | |
private $firstField; | |
private $secondField; | |
public function __get($property) { | |
if (property_exists($this, $property)) { | |
return $this->$property; | |
} | |
} | |
public function __set($property, $value) { | |
if (property_exists($this, $property)) { | |
$this->$property = $value; | |
} | |
return $this; | |
} | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Syntax for extending classes in namespaces is still the same.
Lets call this Object.php:
And now lets create a class called String that extends object in String.php: