Packages

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

Classes

  • NArrayHash
  • NArrayList
  • NCallback
  • NConfigurator
  • NDateTime53
  • NFramework
  • NFreezableObject
  • NImage
  • NObject
  • NObjectMixin

Interfaces

  • IFreezable

Exceptions

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