Skip to content

Instantly share code, notes, and snippets.

@dnaber-de
Created November 25, 2013 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnaber-de/7640893 to your computer and use it in GitHub Desktop.
Save dnaber-de/7640893 to your computer and use it in GitHub Desktop.
Shows the difference between static and dynamic class properties.
<?php
namespace Static_Or_Dynamic;
/**
* a simple property list
*
*/
class Settings_List {
protected $properties = array();
public function set( $key, $value ) {
$this->properties[ $key ] = $value;
}
public function get( $key ) {
return isset( $this->properties[ $key ] )
? $this->properties[ $key ]
: NULL;
}
}
class My_Object {
/**
* @type \Static_Or_Dynamic\List
*/
protected $settings;
public function __construct( Settings_List $settings ) {
$this->settings = $settings;
}
/**
* change the memory limit
*
* @param string $limit
*/
public function set_memory_limit( $limit ) {
$this->settings->set( 'memory_limit', $limit );
}
/**
* get memory limit
*
* @return string
*/
public function get_memory_limit() {
return $this->settings->get( 'memory_limit' );
}
}
$settings = new Settings_List;
$settings->set( 'memory_limit', '40M' );
$cool_feature = new My_Object( $settings );
$better_feature = new My_Object( $settings );
echo $cool_feature->get_memory_limit() . PHP_EOL; # 40M
echo $better_feature->get_memory_limit() . PHP_EOL; # 40M
$cool_feature->set_memory_limit( '80M' );
echo $better_feature->get_memory_limit() . PHP_EOL ; # what would you expect and why?
<?php
/**
* shows the difference between static and dynamic class properties
*
*/
namespace Static_Or_Dynamic;
/**
* a class with a static and a dynamic property
*
* we want to represent devices in a single network domain
*/
class Device {
/**
* network domain
*
* @var string
*/
protected static $domain = 'home_network';
/**
* the type of the device
*
* e.g. pc, tablett, smartphone
*
* @var string
*/
protected $type = '';
/**
* the device name
*
* @var string
*/
protected $name = '';
/**
* create a new device
*
* @param string $type
*/
public function __construct( $type, $name ) {
$this->type = $type;
$this->name = $name;
}
/**
* dynamic setter/getter
*/
public function set_name( $name ) {
$this->name = $name;
}
public function get_name() {
return $this->name;
}
public function get_type() {
return $this->type;
}
/**
* static setter and getter
*/
public static function set_domain( $domain ) {
self::$domain = $domain;
}
public static function get_domain() {
return self::$domain;
}
}
$pc = new Device( 'PC', 'My Desktop Computer' );
$smartphone = new Device( 'Smartphone', 'My Android Phone' );
echo $pc->get_name(); # My Desktop Computer
echo $smartphone->get_name(); # My Android Phone
# Change the name of the PC
$pc->set_name( "Jane's Desktop Computer" );
echo $smartphone->get_name(); # My Android Phone
# now change the network domain
$pc::set_domain( 'office_network' );
echo $smartphone::get_domain(); # what do you expect?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment