Class Object (namespace Nette)


Nette\Object is the ultimate ancestor of all instantiable classes.

It defines some handful methods and enhances object core of PHP:

  • access to undeclared members throws exceptions
  • support for conventional properties with getters and setters
  • support for event raising functionality
  • ability to add new methods to class (extension methods)
Properties is a syntactic sugar which allows access public getter and setter methods as normal object variables. A property is defined by a getter method and optional setter method (no setter method means read-only property).
  1. 1:  $val $obj->label;     // equivalent to $val = $obj->getLabel();
  2. 2:  $obj->label 'Nette';  // equivalent to $obj->setLabel('Nette');
Property names are case-sensitive, and they are written in the camelCaps or PascalCaps.

Event functionality is provided by declaration of property named 'on{Something}' Multiple handlers are allowed.

  1. 1:  public $onClick;                // declaration in class
  2. 2:  $this->onClick['callback';  // attaching event handler
  3. 3:  if (!empty($this->onClick)) ... // are there any handlers?
  4. 4:  $this->onClick($sender$arg);  // raises the event with arguments

Adding method to class (i.e. to all instances) works similar to JavaScript prototype property. The syntax for adding a new method is:

  1. 1:  MyClass::extensionMethod('newMethod'function(MyClass $obj$arg...... });
  2. 2:  $obj new MyClass;
  3. 3:  $obj->newMethod($x);


Author: David Grudl
Copyright: Copyright (c) 2004, 2009 David Grudl
Abstract:
Located: in /Object.php (line 69)
Public Method Summary
static mixed
extensionMethod (string $name, [mixed $callback = NULL])
Adding method to class.
string
Returns the name of the class of this object.
ReflectionObject
Access to reflection.
mixed
__call (string $name, array $args)
Call to undefined method.
static mixed
__callStatic (string $name, array $args)
Call to undefined static method.
& mixed
__get (string $name)
Returns property value. Do not call directly.
bool
__isset (string $name)
Is property defined?
void
__set (string $name, mixed $value)
Sets value of a property. Do not call directly.
void
__unset (string $name)
Access to undeclared property.

Method Details

line 134

extensionMethod

public static mixed extensionMethod (string $name, [mixed $callback = NULL])

Adding method to class.

Input
string $name method name
mixed $callback callback or closure
Output
mixed  

line 77

getClass

public string getClass ()

Returns the name of the class of this object.

Output
string  

line 89

getReflection

public ReflectionObject getReflection ()

Access to reflection.

Output
ReflectionObject  

line 104

__call

public mixed __call (string $name, array $args)

Call to undefined method.

Input
string $name method name
array $args arguments
Output
mixed  
Throws
throws MemberAccessException

line 119

__callStatic

public static mixed __callStatic (string $name, array $args)

Call to undefined static method.

Input
string $name method name (in lower case!)
array $args arguments
Output
mixed  
Throws
throws MemberAccessException

line 153

__get

public mixed & __get (string $name)

Returns property value. Do not call directly.

Input
string $name property name
Output
& mixed property value
Throws
throws MemberAccessException if the property is not defined.

line 181

__isset

public bool __isset (string $name)

Is property defined?

Input
string $name property name
Output
bool  

line 168

__set

public void __set (string $name, mixed $value)

Sets value of a property. Do not call directly.

Input
string $name property name
mixed $value property value
Output
void  
Throws
throws MemberAccessException if the property is not defined or is read-only

line 195

__unset

public void __unset (string $name)

Access to undeclared property.

Input
string $name property name
Output
void  
Throws
throws MemberAccessException