Open PHP5 Object, No need to declare variable name first


/ Published in: PHP
Save to your folder(s)

I use this object verry often, so that I only need to pass one object as parameter on any function. No need to wory when we refactore the function
Usage:
$var = new open();
$var->url = 'http://www.world.com';
$var->title = 'Testing Site';

echo some_function($var);


Copy this code and paste it in your HTML
  1. //PHP5 only//
  2.  
  3. class open
  4. {
  5. var $____x = array();
  6. function __construct($a = array())
  7. {
  8. $this->fromArray($a);
  9. }
  10.  
  11. function __set($name,$val)
  12. {
  13. $this->____x[$name] = $val;
  14. }
  15.  
  16. function __get($name)
  17. {
  18. if(isset($this->____x[$name])) return $this->____x[$name];
  19. }
  20.  
  21. function __isset($name)
  22. {
  23. return isset($this->____x[$name]);
  24. }
  25.  
  26. function toArray()
  27. {
  28. return $this->____x;
  29. }
  30.  
  31. function fromArray($a = array())
  32. {
  33. if(count($a)>0) foreach($a as $k => $v) $this->____x[$k] = $v;
  34. }
  35.  
  36. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.