Overview

Namespaces

  • Jyxo
    • Beholder
      • TestCase
    • Gettext
      • Parser
    • Input
      • Chain
      • Filter
      • Validator
    • Mail
      • Email
        • Attachment
      • Parser
      • Sender
    • Rpc
      • Json
      • Xml
    • Shell
    • Spl
    • Svn
    • Time
    • Webdav
  • PHP

Classes

  • ArrayUtil
  • CountableLimitIterator
  • FilterIterator
  • MapIterator
  • Object
  • ObjectCache

Interfaces

  • ArrayCopy
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: namespace Jyxo\Spl;
 15: 
 16: /**
 17:  * Default object class.
 18:  *
 19:  * @category Jyxo
 20:  * @package Jyxo\Spl
 21:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 22:  * @license https://github.com/jyxo/php/blob/master/license.txt
 23:  * @author Jaroslav HanslĂ­k
 24:  */
 25: class Object implements \Jyxo\Spl\ArrayCopy
 26: {
 27:     /**
 28:      * Returns instance class name.
 29:      *
 30:      * @return string
 31:      */
 32:     public final function getClass()
 33:     {
 34:         return get_class($this);
 35:     }
 36: 
 37:     /**
 38:      * Returns a property value if it has a getter defined.
 39:      *
 40:      * @param string $name Property name
 41:      * @return mixed
 42:      */
 43:     public function &__get($name)
 44:     {
 45:         $class = get_class($this);
 46:         $name = ucfirst($name);
 47: 
 48:         // Return null if no getter is found
 49:         $value = null;
 50: 
 51:         // Tests for possible getters
 52:         static $types = array('get', 'is');
 53:         foreach ($types as $type) {
 54:             $getter = $type . $name;
 55:             if (self::hasMethod($class, $getter)) {
 56:                 // It's necessary to save the value to a variable first because of using &
 57:                 $value = $this->$getter();
 58:                 break;
 59:             }
 60:         }
 61: 
 62:         return $value;
 63:     }
 64: 
 65:     /**
 66:      * Sets property value if it has a setter defined.
 67:      *
 68:      * @param string $name Propety name
 69:      * @param mixed $value Property value
 70:      */
 71:     public function __set($name, $value)
 72:     {
 73:         $setter = 'set' . ucfirst($name);
 74:         if (self::hasMethod(get_class($this), $setter)) {
 75:             $this->$setter($value);
 76:         }
 77:     }
 78: 
 79:     /**
 80:      * Returns if property exists. Property exists if it has defined getter.
 81:      *
 82:      * @param string $name
 83:      * @return boolean
 84:      */
 85:     public function __isset($name)
 86:     {
 87:         $class = get_class($this);
 88:         $name = ucfirst($name);
 89: 
 90:         // Tests for possible getters
 91:         static $types = array('get', 'is');
 92:         foreach ($types as $type) {
 93:             $getter = $type . $name;
 94:             if (self::hasMethod($class, $getter)) {
 95:                 return true;
 96:             }
 97:         }
 98: 
 99:         return false;
100:     }
101: 
102:     /**
103:      * Returns if a class has the given method defined.
104:      *
105:      * @param string $class Class name
106:      * @param string $method Method name
107:      * @return boolean
108:      */
109:     private static function hasMethod($class, $method)
110:     {
111:         static $cache;
112:         if (!isset($cache[$class])) {
113:             $cache[$class] = array_flip(get_class_methods($class));
114:         }
115:         return isset($cache[$class][$method]);
116:     }
117: 
118:     /**
119:      * Converts an object to an array
120:      *
121:      * @return array
122:      */
123:     public function toArray()
124:     {
125:         $values = array();
126:         foreach ((array) $this as $key => $value) {
127:             // Private and protected properties have ugly array key prefixes which we remove
128:             $key = preg_replace('~^.+\0~', '', $key);
129:             $values[$key] = $value;
130:         }
131:         return $values;
132:     }
133: 
134: }
135: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0