Skip to content

Instantly share code, notes, and snippets.

@schkovich
Last active December 22, 2015 05:38
Show Gist options
  • Save schkovich/6424900 to your computer and use it in GitHub Desktop.
Save schkovich/6424900 to your computer and use it in GitHub Desktop.
<?php
/**
* @see http://qafoo.com/blog/016_struct_classes_in_php.html
*/
abstract class Struct
{
/**
* Is run when reading data from inaccessible properties
* @link http://www.php.net/manual/en/language.oop5.overloading.php#object.get
* Overloading __get()
* @param mixed $property
* @throws RuntimeException
*/
public function __get($property)
{
throw new RuntimeException('Trying to get non-existing property ' . $property);
}
/**
* Is run when writing data to inaccessible properties.
* @param string $property
* @param mixed $value
* @link http://www.php.net/manual/en/language.oop5.overloading.php#object.set
* Overloading __set()
* @throws RuntimeException
*/
public function __set($property, $value)
{
throw new RuntimeException('Trying to set non-existing property ' . $property);
}
/**
* Once the cloning is complete, the newly created object's __clone() method
* will be called, to allow any necessary properties that need to be changed.
* @link http://www.php.net/manual/en/language.oop5.cloning.php#object.clone
* Magic methods:: __clone()
*/
public function __clone()
{
foreach ($this as $property => $value) :
if (is_object($value)) :
$this->$property = clone $value;
endif;
endforeach;
}
/**
* This static method is called for classes exported by var_export()
* @link http://www.php.net/manual/en/language.oop5.magic.php#object.set-state
* Magic methods::__set_state()
* @param array $properties array containing exported properties in
* the form array('property' => value, ...).
* @return \Struct
*/
public static function __set_state(array $properties)
{
$struct = new static();
foreach ($properties as $property => $value) :
$struct->$property = $value;
endforeach;
return $struct;
}
}
class TweetUserStruct extends Struct
{
/**
* @var integer
*/
public $user_id;
/**
* @var string
*/
public $screen_name;
/**
* @var integer
*/
public $cursor;
/**
* @var boolean
*/
public $skip_status;
/**
* @var boolean
*/
public $include_user_entities;
/**
* @param integer $user_id
* @param string $screen_name
* @param integer $cursor
* @param bool $skip_status
* @param bool $include_user_entities
*/
public function __construct($user_id = null, $screen_name = null,
$cursor = -1, $skip_status = false, $include_user_entities = false)
{
$this->user_id = $user_id;
$this->screen_name = $screen_name;
$this->cursor = $cursor;
$this->skip_status = $skip_status;
$this->include_user_entities = $include_user_entities;
}
}
class SampleTweetUserApi
{
/**
* @param TweetUserStruct $tweetUser
*/
public function getFriends(TweetUserStruct $tweetUser)
{
// do whatever needed
}
}
$tweetUser = new TweetUserStruct();
$api = new SampleTweetUserApi();
$tweetUser->screen_name = 'phpdrama';
$tweetUser->include_user_entities = true;
$api->getFriends($tweetUser);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment