Created
June 14, 2016 17:01
-
-
Save satsura/6e6b75515a17991595a62c7ef4952991 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
/** | |
*/ | |
abstract class WDT_Class { | |
/** | |
* Set object properties from $args | |
* | |
* @param array $args | |
* @param int $filter | |
* | |
* @return $this | |
*/ | |
function set_properties( $args, $filter = ReflectionProperty::IS_PUBLIC ) { | |
// GEt object properties like array | |
$defaults = $this->get_properties( $filter ); | |
// User can override only public properties | |
foreach ( $args as $key => $value ) { | |
if ( true == key_exists( $key, $defaults ) || false == property_exists( $this, $key ) ) { | |
$this->{$key} = $value; | |
} | |
} | |
return $this; | |
} | |
/** | |
* Get object properties like array | |
* | |
* @param int $filter | |
* | |
* @return array | |
*/ | |
function get_properties( $filter = ReflectionProperty::IS_PUBLIC ) { | |
$args = array(); | |
$reflect = new ReflectionObject( $this ); | |
$properties = $reflect->getProperties( $filter ); | |
foreach ( $properties as $property ) { | |
$args[$property->getName()] = $property->getValue( $this ); | |
} | |
return $args; | |
} | |
function __call( $name, $args ) { | |
$name = "_{$name}"; | |
if ( ! $args ) { | |
if ( property_exists( $this, $name ) ) { | |
return $this->$name; | |
} | |
return null; | |
} | |
return $this->$name = ( count( $args ) == 1 ) ? array_shift( $args ) : $args; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment