...

View Full Version : overridding



xiaodao
01-24-2010, 06:03 AM
Hi

We know in JAVA, overridding is simple, how to do that in PHP? i searched online, only found examples on child class inherit parent then override, any possibility to do overridding in the same class? please give example, thank you

bdl
01-24-2010, 06:28 AM
The PHP manual has an extensive section on classes and objects (http://us.php.net/manual/en/language.oop5.php).

xiaodao
01-24-2010, 06:52 AM
already read, but i got this

3) Sadly, overriding methods is only possible with the Zend II engine via Inheritance or Polymorphism, ( and __construct() can only be defined within a class). If you want to override a method in the same class, my suggestion is to provide the method signature with a $flag = null variable, which you call a SWITCH on to pick what the data should do

which i do not understand

bdl
01-24-2010, 07:30 AM
So you can see there are limitations (at least in this version).



my suggestion is to provide the method signature with a $flag = null variable, which you call a SWITCH on to pick what the data should do

which i do not understand


Ok, this is very simple. Here's an example (UNTESTED):


class SomeClass
{
function some_method( $flag= null ) {
if ( is_null($flag) ) {
return 'default value';
} else {
switch($flag) {
case 'foo':
return 'foo passed';
case 'bar':
return 'bar passed';
default:
return "args: 'foo' | 'bar'";
}
}
}

}


The method's signature has an argument with a default value, null. If you decide to call it with no arg, e.g. $obj->some_method(), then the default action is taken. If you pass an arg, e.g. $obj->some_method('foo'), then the switch statement triggers the appropriate action.

I'm sure there are examples using func_get_args() (http://us.php.net/manual/en/function.func-get-args.php) as well.

xiaodao
01-25-2010, 01:44 AM
....thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum