Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

  • ArrayHash
  • ArrayList
  • Callback
  • Configurator
  • DateTime
  • Framework
  • FreezableObject
  • Image
  • Object
  • ObjectMixin

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidArgumentException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • OutOfRangeException
  • StaticClassException
  • UnexpectedValueException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * Nette\Object behaviour mixin.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: final class ObjectMixin
 24: {
 25:     /** @var array */
 26:     private static $methods;
 27: 
 28: 
 29: 
 30:     /**
 31:      * Static class - cannot be instantiated.
 32:      */
 33:     final public function __construct()
 34:     {
 35:         throw new StaticClassException;
 36:     }
 37: 
 38: 
 39: 
 40:     /**
 41:      * Call to undefined method.
 42:      * @param  object
 43:      * @param  string  method name
 44:      * @param  array   arguments
 45:      * @return mixed
 46:      * @throws MemberAccessException
 47:      */
 48:     public static function call($_this, $name, $args)
 49:     {
 50:         $class = new Reflection\ClassType($_this);
 51: 
 52:         if ($name === '') {
 53:             throw new MemberAccessException("Call to class '$class->name' method without name.");
 54:         }
 55: 
 56:         // event functionality
 57:         if ($class->hasEventProperty($name)) {
 58:             if (is_array($list = $_this->$name) || $list instanceof \Traversable) {
 59:                 foreach ($list as $handler) {
 60:                     callback($handler)->invokeArgs($args);
 61:                 }
 62:             }
 63:             return NULL;
 64:         }
 65: 
 66:         // extension methods
 67:         if ($cb = $class->getExtensionMethod($name)) {
 68:             array_unshift($args, $_this);
 69:             return $cb->invokeArgs($args);
 70:         }
 71: 
 72:         throw new MemberAccessException("Call to undefined method $class->name::$name().");
 73:     }
 74: 
 75: 
 76: 
 77:     /**
 78:      * Call to undefined static method.
 79:      * @param  object
 80:      * @param  string  method name
 81:      * @param  array   arguments
 82:      * @return mixed
 83:      * @throws MemberAccessException
 84:      */
 85:     public static function callStatic($class, $name, $args)
 86:     {
 87:         throw new MemberAccessException("Call to undefined static method $class::$name().");
 88:     }
 89: 
 90: 
 91: 
 92:     /**
 93:      * Returns property value.
 94:      * @param  object
 95:      * @param  string  property name
 96:      * @return mixed   property value
 97:      * @throws MemberAccessException if the property is not defined.
 98:      */
 99:     public static function & get($_this, $name)
100:     {
101:         $class = get_class($_this);
102: 
103:         if ($name === '') {
104:             throw new MemberAccessException("Cannot read a class '$class' property without name.");
105:         }
106: 
107:         if (!isset(self::$methods[$class])) {
108:             // get_class_methods returns ONLY PUBLIC methods of objects
109:             // but returns static methods too (nothing doing...)
110:             // and is much faster than reflection
111:             // (works good since 5.0.4)
112:             self::$methods[$class] = array_flip(get_class_methods($class));
113:         }
114: 
115:         // property getter support
116:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
117:         $m = 'get' . $name;
118:         if (isset(self::$methods[$class][$m])) {
119:             // ampersands:
120:             // - uses &__get() because declaration should be forward compatible (e.g. with Nette\Utils\Html)
121:             // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
122:             $val = $_this->$m();
123:             return $val;
124:         }
125: 
126:         $m = 'is' . $name;
127:         if (isset(self::$methods[$class][$m])) {
128:             $val = $_this->$m();
129:             return $val;
130:         }
131: 
132:         $type = isset(self::$methods[$class]['set' . $name]) ? 'a write-only' : 'an undeclared';
133:         $name = func_get_arg(1);
134:         throw new MemberAccessException("Cannot read $type property $class::\$$name.");
135:     }
136: 
137: 
138: 
139:     /**
140:      * Sets value of a property.
141:      * @param  object
142:      * @param  string  property name
143:      * @param  mixed   property value
144:      * @return void
145:      * @throws MemberAccessException if the property is not defined or is read-only
146:      */
147:     public static function set($_this, $name, $value)
148:     {
149:         $class = get_class($_this);
150: 
151:         if ($name === '') {
152:             throw new MemberAccessException("Cannot write to a class '$class' property without name.");
153:         }
154: 
155:         if (!isset(self::$methods[$class])) {
156:             self::$methods[$class] = array_flip(get_class_methods($class));
157:         }
158: 
159:         // property setter support
160:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
161: 
162:         $m = 'set' . $name;
163:         if (isset(self::$methods[$class][$m])) {
164:             $_this->$m($value);
165:             return;
166:         }
167: 
168:         $type = isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name])
169:             ? 'a read-only' : 'an undeclared';
170:         $name = func_get_arg(1);
171:         throw new MemberAccessException("Cannot write to $type property $class::\$$name.");
172:     }
173: 
174: 
175: 
176:     /**
177:      * Throws exception.
178:      * @param  object
179:      * @param  string  property name
180:      * @param  mixed   property value
181:      * @throws MemberAccessException
182:      */
183:     public static function remove($_this, $name)
184:     {
185:         $class = get_class($_this);
186:         throw new MemberAccessException("Cannot unset the property $class::\$$name.");
187:     }
188: 
189: 
190: 
191:     /**
192:      * Is property defined?
193:      * @param  object
194:      * @param  string  property name
195:      * @return bool
196:      */
197:     public static function has($_this, $name)
198:     {
199:         if ($name === '') {
200:             return FALSE;
201:         }
202: 
203:         $class = get_class($_this);
204:         if (!isset(self::$methods[$class])) {
205:             self::$methods[$class] = array_flip(get_class_methods($class));
206:         }
207: 
208:         $name[0] = $name[0] & "\xDF";
209:         return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
210:     }
211: 
212: }
213: 
Nette Framework 2.0beta1 API API documentation generated by ApiGen 2.3.0