Packages

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

Classes

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

Interfaces

  • IFreezable

Exceptions

  • ArgumentOutOfRangeException
  • DeprecatedException
  • DirectoryNotFoundException
  • FatalErrorException
  • FileNotFoundException
  • InvalidStateException
  • IOException
  • MemberAccessException
  • NotImplementedException
  • NotSupportedException
  • NStaticClassException
  • NUnknownImageFileException
  • 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 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:             } elseif ($list !== NULL) {
 61:                 throw new UnexpectedValueException("Property $class->name::$$name must be array or NULL, " . gettype($list) ." given.");
 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 method.
 79:      * @param  object
 80:      * @param  string  method name
 81:      * @param  array   arguments
 82:      * @return mixed
 83:      * @throws MemberAccessException
 84:      */
 85:     public static function callProperty($_this, $name, $args)
 86:     {
 87:         if (strlen($name) > 3) {
 88:             $op = substr($name, 0, 3);
 89:             $prop = strtolower($name[3]) . substr($name, 4);
 90:             if ($op === 'add' && property_exists($_this, $prop.'s')) {
 91:                 $_this->{$prop.'s'}[] = $args[0];
 92:                 return $_this;
 93: 
 94:             } elseif ($op === 'set' && property_exists($_this, $prop)) {
 95:                 $_this->$prop = $args[0];
 96:                 return $_this;
 97: 
 98:             } elseif ($op === 'get' && property_exists($_this, $prop)) {
 99:                 return $_this->$prop;
100:             }
101:         }
102:         self::call($_this, $name, $args);
103:     }
104: 
105: 
106: 
107:     /**
108:      * Call to undefined static method.
109:      * @param  string
110:      * @param  string  method name
111:      * @param  array   arguments
112:      * @return mixed
113:      * @throws MemberAccessException
114:      */
115:     public static function callStatic($class, $name, $args)
116:     {
117:         throw new MemberAccessException("Call to undefined static method $class::$name().");
118:     }
119: 
120: 
121: 
122:     /**
123:      * Returns property value.
124:      * @param  object
125:      * @param  string  property name
126:      * @return mixed   property value
127:      * @throws MemberAccessException if the property is not defined.
128:      */
129:     public static function & get($_this, $name)
130:     {
131:         $class = get_class($_this);
132: 
133:         if ($name === '') {
134:             throw new MemberAccessException("Cannot read a class '$class' property without name.");
135:         }
136: 
137:         if (!isset(self::$methods[$class])) {
138:             // get_class_methods returns ONLY PUBLIC methods of objects
139:             // but returns static methods too (nothing doing...)
140:             // and is much faster than reflection
141:             // (works good since 5.0.4)
142:             self::$methods[$class] = array_flip(get_class_methods($class));
143:         }
144: 
145:         // public method as closure getter
146:         if (isset(self::$methods[$class][$name])) {
147:             $val = create_function('', 'extract(NCFix::$vars['.NCFix::uses(array('_this'=>$_this,'name'=> $name)).'], EXTR_REFS);
148:                 return call_user_func_array(array($_this, $name), func_get_args());
149:             ');
150:             return $val;
151:         }
152: 
153:         // property getter support
154:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
155:         $m = 'get' . $name;
156:         if (isset(self::$methods[$class][$m])) {
157:             // ampersands:
158:             // - uses &__get() because declaration should be forward compatible (e.g. with NHtml)
159:             // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
160:             $val = $_this->$m();
161:             return $val;
162:         }
163: 
164:         $m = 'is' . $name;
165:         if (isset(self::$methods[$class][$m])) {
166:             $val = $_this->$m();
167:             return $val;
168:         }
169: 
170:         $type = isset(self::$methods[$class]['set' . $name]) ? 'a write-only' : 'an undeclared';
171:         $name = func_get_arg(1);
172:         throw new MemberAccessException("Cannot read $type property $class::\$$name.");
173:     }
174: 
175: 
176: 
177:     /**
178:      * Sets value of a property.
179:      * @param  object
180:      * @param  string  property name
181:      * @param  mixed   property value
182:      * @return void
183:      * @throws MemberAccessException if the property is not defined or is read-only
184:      */
185:     public static function set($_this, $name, $value)
186:     {
187:         $class = get_class($_this);
188: 
189:         if ($name === '') {
190:             throw new MemberAccessException("Cannot write to a class '$class' property without name.");
191:         }
192: 
193:         if (!isset(self::$methods[$class])) {
194:             self::$methods[$class] = array_flip(get_class_methods($class));
195:         }
196: 
197:         // property setter support
198:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
199: 
200:         $m = 'set' . $name;
201:         if (isset(self::$methods[$class][$m])) {
202:             $_this->$m($value);
203:             return;
204:         }
205: 
206:         $type = isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name])
207:             ? 'a read-only' : 'an undeclared';
208:         $name = func_get_arg(1);
209:         throw new MemberAccessException("Cannot write to $type property $class::\$$name.");
210:     }
211: 
212: 
213: 
214:     /**
215:      * Throws exception.
216:      * @param  object
217:      * @param  string  property name
218:      * @return void
219:      * @throws MemberAccessException
220:      */
221:     public static function remove($_this, $name)
222:     {
223:         $class = get_class($_this);
224:         throw new MemberAccessException("Cannot unset the property $class::\$$name.");
225:     }
226: 
227: 
228: 
229:     /**
230:      * Is property defined?
231:      * @param  object
232:      * @param  string  property name
233:      * @return bool
234:      */
235:     public static function has($_this, $name)
236:     {
237:         if ($name === '') {
238:             return FALSE;
239:         }
240: 
241:         $class = get_class($_this);
242:         if (!isset(self::$methods[$class])) {
243:             self::$methods[$class] = array_flip(get_class_methods($class));
244:         }
245: 
246:         $name[0] = $name[0] & "\xDF";
247:         return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
248:     }
249: 
250: }
251: 
Nette Framework 2.0.4 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.7.0