Last active
November 22, 2017 16:30
-
-
Save emmanuelbarturen/91e5081f9ce72da7d971f30c60049641 to your computer and use it in GitHub Desktop.
Create Accesors like laravel in object
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
class TheAttribute | |
{ | |
public function __get($name) | |
{ | |
$name = array_map('ucfirst', explode('_', $name)); | |
$name = implode($name); | |
# use echo instead return if is not a framework | |
return $this->{'get' . $name . 'Attribute'}(); | |
} | |
public function getTheNameAttribute() | |
{ | |
return 'Emmanuel Barturen'; | |
} | |
} | |
(new TheAttribute())->the_name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment