Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: overridding

  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    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

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    The PHP manual has an extensive section on classes and objects.

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    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

  • #4
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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):
    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 08:36 AM. Reason: Added a default switch action

  • #5
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    ....thanks
    flying dagger


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •