Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Created September 14, 2016 12:18
Show Gist options
  • Save kirilkirkov/ad4e12d0149c6ebaa42b74fd78bd6b08 to your computer and use it in GitHub Desktop.
Save kirilkirkov/ad4e12d0149c6ebaa42b74fd78bd6b08 to your computer and use it in GitHub Desktop.
PHP Magic methods - Getters and Setters
<?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;
}
}
?>
@kirilkirkov
Copy link
Author

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment