Source of file GettersAndSetters.php
Size: 1,562 Bytes - Last Modified: 2020-07-03T12:08:09+00:00
/var/www/docs.ssmods.com/process/src/src/Traits/GettersAndSetters.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | <?php namespace Sunnysideup\UpgradeToSilverstripe4\Traits; trait GettersAndSetters { /** * creates magic getters and setters * if you call $this->getFooBar() then it will get the variable FooBar even if the method * getFooBar does not exist. * * if you call $this->setFooBar('hello') then it will set the variable FooBar even if the method * setFooBar does not exist. * * See: http://php.net/manual/en/language.oop5.overloading.php#object.call * * @param string $function name of the function * @param array $args parameters provided to the getter / setter */ public function __call($function, $args) { $getOrSet = substr($function, 0, 3); if ($getOrSet === 'set' || $getOrSet === 'get') { $var = lcfirst(ltrim($function, $getOrSet)); if (property_exists($this, $var)) { if ($getOrSet === 'get') { return $this->{$var}; } elseif ($getOrSet === 'set') { $this->{$var} = $args[0]; return $this; } } else { user_error( 'Fatal error: can not get/set variable in ModuleUpgraderBaseWithVariables::' . $var, E_USER_ERROR ); } } else { user_error( 'Fatal error: Call to undefined method ModuleUpgraderBaseWithVariables::' . $function . '()', E_USER_ERROR ); } } } |