Posts filed under 'ZEND PHP5 Exam'
Static Method Inheritance
We know static methods are callable without instantiating objects. But if it is used from explicit class name, php violates inheritance rule by referencing the current class instead of the called class. Moreover if we use ’self’ to call the method , it will represent the current class.
Lets see the following code
class BaseClass
{
static function dump()
{
echo __CLASS__ . ‘::dump()’;
}
static function callDump()
{
self::dump(); // self represents the current class
}
}
class ChildClass extends BaseClass
{
static function dump()
{
echo __CLASS__. ‘::dump()’;
}
}
ChildClass::callDump();
In the above code , what the inheritance rule says , it says that the output is supposed to ChildClass::dump() but real output is BaseClass::dump(). This is because ’self’ always represents the current class.
To solve this limitation PHP introduce Late Static Binding (LSB) from PHP version 5.3.0 that can be used to reference the current class. It is represented as ’static::’. So the above code will be as following
class BaseClass
{
static function dump()
{
echo __CLASS__ . ‘::dump()’;
}
static function callDump()
{
static::dump(); // static refers the called class
}
}
class ChildClass extends BaseClass
{
static function dump()
{
echo __CLASS__. ‘::dump()’;
}
}
ChildClass::callDump();
Now the above code will output ChildClass::dump(), but ’static::’ is available from PHP version 5.3.0
For more info please visit the following links
Static Keyworkd
Late Static Binding
Thank$
$haymoL
eXpLoRinG PHP
Add comment December 25, 2007
Operator Precedence in PHP
The precedence of an operator specifies how “tightly” it binds two expressions together. Lets play with few of them
- ‘[' is left associative and has higher precedence than assignment operator '='
function one($str)
{
echo "$str";
return 1;
}
$a = array(1,2);
$a[one("A")] = $a[one("B")] = 1; // outputs AB - Assignment Operators are right associative
function output(& $b)
{
echo( “b value is: “.$b );
return $b;
}
$a = 2;
$b = 5;
echo ($a = $b = 1);
$a = 2;
$b = 5;
echo ( “a value is: “.$a = output($b) );
?>
Output is:
1
b value is: 5
a value is: 5
- Comparison Operator always returns TRUE/FALSE
$b = 5;
$a = ( ( ++$b ) > 5 ); // Pre-increment test
echo (int)$a; // Output 1
$b = 5;
$a = ( ( $b++ ) > 5 ); // Post-increment test
echo (int)$a; // Output 0
- assignment operator has higher precedence than bitwise operator
( 9 & 8 == 8 ) ? ‘true’:'false’; //same as 9 & ( 8 == 8 ) which is true
( 8 & 8 == 8 ) ? ‘true’:'false’; //same as 8 & ( 8 == 8 ) which is false
- Logical Operator Precedence : and, or, nor, !, &&, ||
$a && $b || $c; // equivalent to(a and b) or c
$a AND $b || $c; // equivalent toa and (b or c)
again
$a = $b && $c; // equivalent to $a = ($b && $c)
$a = $b AND $c; //equivalent to ($a =$b) && $c
- The ternary operator is a statement, and that it doesn’t evaluate to a variable, but to the result of a statement
echo (true?’true’:false?’t':’f'); // equivalent to echo ((true ? ‘true’ : ‘false’) ? ‘t’ : ‘f’);
Reference : PHP Manual
1 comment December 16, 2007




