3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait GetterAndSetter { /** * @var string */ private $setterPrefix = 'set'; /** * @var string */ private $getterPrefix = 'get'; /** * Assigns value to property by attempting to find its * setter. If no setter exists the value is casted into * appropriate type and assigned to the property directly. * * @param $key * @param $value * @return $this */ function __set($key, $value) { $expectedSetter = "{$this->setterPrefix}{$key}"; if ( method_exists($this, $expectedSetter) ) { $this->{$expectedSetter}($value); } elseif ( property_exists($this, $key) ){ $enforcedType = gettype($this->{$key}); $this->{$key} = $value; settype($this->{$key}, $enforcedType); } return $this; } /** * Invokes matching getter if defined * otherwise returns property as is. * * @param $key * @return mixed */ function __get($key) { $expectedGetter = $this->getterPrefix . $key; var_dump(method_exists($this, $expectedGetter)); if ( method_exists($this, $expectedGetter) ) { return $this->{$expectedGetter}(); } return $this->{$key}; } /** * Resolves setClassProperty($val) to $obj->classProperty = $val, * casts appropriate type to value and assigns * to property * * @param $method string Name of possible setter method * @param $val mixed Value to assign to property * @return $this */ function __call($method, $val=[]) { $possibleKey = lcfirst(str_replace($this->setterPrefix, '', $method)); if ( property_exists($this, $possibleKey) ){ $enforcedType = gettype($this->{$possibleKey}); $this->{$possibleKey} = $val[0]; settype($this->{$possibleKey}, $enforcedType); } return $this; } } class AuthorizeNetCustomer { //Will call setters when available otherwise //value will be casted to the property's type //and assigned. use GetterAndSetter; private $merchantCustomerId; private $description; private $email; private $paymentProfiles = array(); private $shipToList = array(); private $customerProfileId; function __construct() { $this->merchantCustomerId = (int) $this->merchantCustomerId; $this->customerProfileId = (int) $this->customerProfileId; $this->description = (string) $this->description; $this->email = (string) $this->email; } public function setPaymentProfiles(array $val){ $this->paymentProfiles = $val; } public function setShipToList(array $val){ $this->shipToList = $val; } public function getShipToList() { return 'adasd'; } } $a = new AuthorizeNetCustomer(); $a->setEmail('cgrandos@delivery.com'); $a->merchantCustomerId = '1'; $a->setCustomerProfileId('34') ->setDescription(232323) ->setShipToList([1,2,3]); // var_dump($a->email); // var_dump($a->merchantCustomerId); // var_dump($a->customerProfileId); // var_dump($a->description); var_dump($a->shipToList);
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
bool(true) string(5) "adasd"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/jBgC3 on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/jBgC3 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/jBgC3 on line 3
Process exited with code 255.

preferences:
203.2 ms | 401 KiB | 326 Q