Skip to content

Instantly share code, notes, and snippets.

@Jarvvski
Created April 25, 2018 09:32
Show Gist options
  • Select an option

  • Save Jarvvski/7ff0fb910366bc23ad28e68d640689e7 to your computer and use it in GitHub Desktop.

Select an option

Save Jarvvski/7ff0fb910366bc23ad28e68d640689e7 to your computer and use it in GitHub Desktop.
<?php
class MyClass {
protected $prop1;
protected $prop2;
public function set(string $prop, $value) {
if(property_exists($this, $prop)) {
$this->$prop = $value;
return;
}
return;
}
public function get(string $prop) {
if (property_exists($this, $prop)) {
return $this->$prop;
}
return null;
}
public static function create(array $data) {
$obj = new self;
$obj->update($data);
return $obj;
}
public function update(array $data) {
foreach ($data as $key => $value) {
$this->set($key, $value);
}
}
public function printProps() {
$props = get_object_vars($this);
foreach ($props as $key => $value) {
printf("property %s = $value\n", $key);
}
}
}
$obj = MyClass::create(['prop1' => 12, 'prop2' => "stuff"]);
$var = $obj->get('prop1');
printf("\n\n---Step 1---\n");
$obj->printProps();
$obj->update(['prop2' => "adam"]);
printf("\n\n---Step 2---\n");
$obj->printProps();
$obj->update(['prop2' => "Bill", 'prop1' => 1000000]);
printf("\n\n---Step 3---\n");
$obj->printProps();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment