Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2010, 06:03 AM   PM User | #1
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
overridding

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
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 01-24-2010, 06:28 AM   PM User | #2
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
The PHP manual has an extensive section on classes and objects.
bdl is offline   Reply With Quote
Old 01-24-2010, 06:52 AM   PM User | #3
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
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
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 01-24-2010, 07:30 AM   PM User | #4
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
So you can see there are limitations (at least in this version).

Quote:
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):
PHP Code:
class SomeClass
{
    function 
some_method$flagnull ) {
        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() as well.

Last edited by bdl; 01-24-2010 at 07:36 AM.. Reason: Added a default switch action
bdl is offline   Reply With Quote
Old 01-25-2010, 01:44 AM   PM User | #5
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
....thanks
__________________
flying dagger
xiaodao is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:27 AM.


Advertisement
Log in to turn off these ads.