Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • NAmbiguousServiceException
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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 LogicException("Cannot instantiate static class " . get_class($this));
 34:     }
 35: 
 36: 
 37: 
 38:     /**
 39:      * Call to undefined method.
 40:      * @param  string  method name
 41:      * @param  array   arguments
 42:      * @return mixed
 43:      * @throws MemberAccessException
 44:      */
 45:     public static function call($_this, $name, $args)
 46:     {
 47:         $class = new NClassReflection($_this);
 48: 
 49:         if ($name === '') {
 50:             throw new MemberAccessException("Call to class '$class->name' method without name.");
 51:         }
 52: 
 53:         // event functionality
 54:         if ($class->hasEventProperty($name)) {
 55:             if (is_array($list = $_this->$name) || $list instanceof Traversable) {
 56:                 foreach ($list as $handler) {
 57:                     callback($handler)->invokeArgs($args);
 58:                 }
 59:             }
 60:             return NULL;
 61:         }
 62: 
 63:         // extension methods
 64:         if ($cb = $class->getExtensionMethod($name)) {
 65:             array_unshift($args, $_this);
 66:             return $cb->invokeArgs($args);
 67:         }
 68: 
 69:         throw new MemberAccessException("Call to undefined method $class->name::$name().");
 70:     }
 71: 
 72: 
 73: 
 74:     /**
 75:      * Returns property value.
 76:      * @param  string  property name
 77:      * @return mixed   property value
 78:      * @throws MemberAccessException if the property is not defined.
 79:      */
 80:     public static function & get($_this, $name)
 81:     {
 82:         $class = get_class($_this);
 83: 
 84:         if ($name === '') {
 85:             throw new MemberAccessException("Cannot read a class '$class' property without name.");
 86:         }
 87: 
 88:         if (!isset(self::$methods[$class])) {
 89:             // get_class_methods returns ONLY PUBLIC methods of objects
 90:             // but returns static methods too (nothing doing...)
 91:             // and is much faster than reflection
 92:             // (works good since 5.0.4)
 93:             self::$methods[$class] = array_flip(get_class_methods($class));
 94:         }
 95: 
 96:         // property getter support
 97:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
 98:         $m = 'get' . $name;
 99:         if (isset(self::$methods[$class][$m])) {
100:             // ampersands:
101:             // - uses &__get() because declaration should be forward compatible (e.g. with NHtml)
102:             // - doesn't call &$_this->$m because user could bypass property setter by: $x = & $obj->property; $x = 'new value';
103:             $val = $_this->$m();
104:             return $val;
105:         }
106: 
107:         $m = 'is' . $name;
108:         if (isset(self::$methods[$class][$m])) {
109:             $val = $_this->$m();
110:             return $val;
111:         }
112: 
113:         $name = func_get_arg(1);
114:         throw new MemberAccessException("Cannot read an undeclared property $class::\$$name.");
115:     }
116: 
117: 
118: 
119:     /**
120:      * Sets value of a property.
121:      * @param  string  property name
122:      * @param  mixed   property value
123:      * @return void
124:      * @throws MemberAccessException if the property is not defined or is read-only
125:      */
126:     public static function set($_this, $name, $value)
127:     {
128:         $class = get_class($_this);
129: 
130:         if ($name === '') {
131:             throw new MemberAccessException("Cannot assign to a class '$class' property without name.");
132:         }
133: 
134:         if (!isset(self::$methods[$class])) {
135:             self::$methods[$class] = array_flip(get_class_methods($class));
136:         }
137: 
138:         // property setter support
139:         $name[0] = $name[0] & "\xDF"; // case-sensitive checking, capitalize first character
140:         if (isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name])) {
141:             $m = 'set' . $name;
142:             if (isset(self::$methods[$class][$m])) {
143:                 $_this->$m($value);
144:                 return;
145: 
146:             } else {
147:                 $name = func_get_arg(1);
148:                 throw new MemberAccessException("Cannot assign to a read-only property $class::\$$name.");
149:             }
150:         }
151: 
152:         $name = func_get_arg(1);
153:         throw new MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
154:     }
155: 
156: 
157: 
158:     /**
159:      * Is property defined?
160:      * @param  string  property name
161:      * @return bool
162:      */
163:     public static function has($_this, $name)
164:     {
165:         if ($name === '') {
166:             return FALSE;
167:         }
168: 
169:         $class = get_class($_this);
170:         if (!isset(self::$methods[$class])) {
171:             self::$methods[$class] = array_flip(get_class_methods($class));
172:         }
173: 
174:         $name[0] = $name[0] & "\xDF";
175:         return isset(self::$methods[$class]['get' . $name]) || isset(self::$methods[$class]['is' . $name]);
176:     }
177: 
178: }
179: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0