Posts filed under 'PHP'

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

PHP as Apache module

We can use php.ini directives configuration settings such as error_reporting, display_errors, auto_prepend_file etc from apache configuration files (httd.conf or .htaccess file).

Lets we want to append a config file (ex. init.php) at the beginning of a script(index.php). So we need to add the following statement in our .htaccess file.


1. init.php sample file
<?
define(”DOCUMENT_ROOT” , $_SERVER['DOCUMENT_ROOT']);
define(”DISPLAY_ERROR” , 1);
?>


2. index.php sample file
<?
print_r(DOCUMENT_ROOT);
?>


3..htaccess file
#php_value name value
#Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.

php_value auto_prepend_file init.php

N.B. All the files needs to should be store in the same directory as we use htaccess which used as per directory level. So all the php scripts in that directory will prepend the init.php file.

Now we are ready to test the script.

There are lots of php ini directives that can be altered from apache module.

For more info please visit the php manual

Cheer$
$haymoL
eXplOrinG PHP


Add comment December 18, 2007


Me in Brief

Expert Rating Certified Professional PHP5
Hi I am Shaikhul Islam Chowdhury Shaymol,
Currently working at Internet BS Corp as a Software Engineer.
I like to explore LAMP and other Web Technologies.
 

My LinkedIn Profile

View Shaikhul Islam Chowdhury Shaymol's profile on LinkedIn
 

Pages

Categories

Links

RSS PHP Magazine

RSS PHPGeek

Archives

Photos from FlickR

graduate

RoomMates@RashidHall.KUET

faruk monir monil n me

yamin me n tani apu

me@home

More Photos