Obsah
(this page is translated by Google; We're working hard on a human translation)
Nette\Object
Nette\Object is the common ancestor of all classes Nette Framework. In other programming languages such a class exists automatically (in Pascal TObject , DOT.NET is System.Object and Java or Ruby to Object ), but PHP does not exist. It is a class sufficiently transparent, it should not cause any conflict, because it can be a base class for your class to use yours.
class MyClass extends Nette\Object
{
} Reflections
Base class provides easy access to the reflection method getReflection() (returns Nette\Reflection\ClassReflection ):
$object = new MyClass();
$res = $object ->getReflection()->hasMethod( 'test' ); // má třída metodu test?
$className = $object ->getReflection()->getName(); // zjistí jméno třídy Remedying inconsistent generating errors
PHP responds to the access of undeclared members of the highly inconsistent:
$obj ->undeclared = 1 ; // projde bez hlášení
echo $obj ->undeclared2; // generuje Notice
MyClass:: $undeclared = 1 ; // generuje Fatal error
$obj ->undeclared(); // generuje Fatal error Usually this is a typos, which are difficult to source nalezitelných errors.
Objects of classes that are descendants Nette\Object , always on access to member nedeklarovanému throw an exception MemberAccessException .
Getter and setter
The term property shall become members of special classes that allow to work with methods such as if they were variables. For Nette\Object in the case of Getter and setter. From the outside looks like an ordinary variable, but we have access to it fully under control. We can validate inputs or the outputs and, if necessary.
- Getter and setter methods must be public.
- Getter is mandatory, optional setter (in this case, then we get a variable read-only, write-only variables are not supported).
- Getter assumes no parameter, just one setter - the new value.
- Names are case insensitive (case sensitive). The first letter of property can be big or small, but it should be an English letter or underscore.
class Circle extends Nette\Object
{
private $radius ;
public function getRadius()
{
return $this ->radius;
}
public function setRadius( $radius )
{
// hodnotu před uložením validujeme
$this ->radius = max ( 0 , (float) $radius );
}
public function getArea()
{
return $this ->radius * $this ->radius * M_PI;
}
}
$circle = new Circle();
$circle ->radius = 12 ; // volání $circle->setRadius(12);
echo 'Radius: ' . $circle ->radius; //volání $circle->getRadius();
echo 'Area: ' . $circle ->area; //volání $circle->getArea() Moreover, if we add a circle property filled / empty, more natural than $circle->getFilled() call is $circle->isFilled() . I therefore accept Nette both forms, and if there getFilled() call if she is - takes precedence.
class FillableCircle extends Circle
{
private $filled ;
public function isFilled()
{
return $this ->filled;
}
public function setFilled( $filled )
{
//opět validace
$this ->filled = (bool) $filled ;
}
}
$circle = new FillableCircle();
$circle ->filled = TRUE ; //volání $circle->setFilled(TRUE);
echo 'Filled: ' . $circle ->filled; //volání $circle->isFilled(); This is only a veneer of syntactic, whose significance is merely clarifying the code. If you do not, so you do not need to use properties.
Properties can be used to simplify the example above, the self-reflection:
$className = $object ->reflection->name; //namísto $object->getReflection()->getName() Events
If you need to call at any time more functions with the same parameters, they can throw me events (events).
class Zahrada extends Nette\Object
{
//deklarace ve třídě
public $onSound ;
}
function kocka( $zvuk )
{
echo $zvuk === 'haf' ? 'lek' : '' ;
}
function pes( $zvuk )
{
echo $zvuk === 'mňau' || $zvuk === 'haf' ? 'haf' : '' ;
}
$zahrada = new Zahrada;
// nastavím handlery
$zahrada ->onSound[] = 'kocka' ;
$zahrada ->onSound[] = 'pes' ;
$zahrada ->onSound[] = function ( $zvuk ) { //handlerem může být i anonymní funkce
echo strlen ( $zvuk ) > 2 ? 'haf' : 'lek' ;
};
$zahrada ->onSound( 'haf' ); // vypíše lekhafhaf Extension Methods
If you want to finish a class method, and for some reason you can not realize poděděním, so you can use the extension method.
class MyClass extends Nette\Object
{
public $a ;
public $b ;
}
// deklarace budoucí metody MyClass::join()
function MyClass_join(MyClass $_this , $separator )
{
return $_this ->a . $separator . $_this ->b;
}
MyClass::extensionMethod( 'join' , 'MyClass_join' );
MyClass::extensionMethod( 'repeatB' , function (MyClass $_this , $times ) {
return str_repeat ( $_this ->b, $times );
});
$obj = new MyClass();
echo $obj -> join ( ' ' );
echo $obj ->repeatB( 5 ); See also:




PJK | 1. 9. 2010, 0:14 | comment
Malá technická: V Pascalu TObject není, vlastně tam není žádný object :D. Pascal není Object Pascal / Delphi.