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 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Pennsylvania, United States
    Posts
    54
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question final method -> object oriented programming

    Recently I've been looking into object oriented programming (OOP) and I've picked up on it rather fast. The only thing that I'm confused on is, the final method. By "override", does that mean, for example if function doWrite() exists in class myClass it can not be "replace with" function doWrite() in mySecondClass? What exacty does "override" mean, rather stupid question, sorry.

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    nice example by slorenzo comment on php.net

    PHP Code:
    <?php
    class parentClass {
        public function 
    someMethod() { }
    }
    class 
    childClass extends parentClass {
        public final function 
    someMethod() { } //override parent function
    }

    $class = new childClass;
    $class->someMethod(); //call the override function in chield class
    ?>
    Well, now when i look at it, it's not so nice at all, should be.

    When i look at it a bit, it looks like class naming restriction rather than anything else
    Last edited by funnymoney; 07-13-2009 at 10:36 PM.

  • Users who have thanked funnymoney for this post:

    johnnnn (07-14-2009)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Override exists to allow runtime binding of methods based on the object in use. This is tougher to see easily in PHP since its datatype weak, so we'll use an example with typehinting a function with an interface:
    PHP Code:
    interface IMyInterface
    {
        public function 
    myMethod();
    }

    class 
    Parent implements IMyInterface
    {
        
    // Parent is typeof IMyInterface
        
    public function myMethod()
        {
            print 
    __METHOD__;
        }
    }

    class 
    Child extends Parent
    {
        
    // Child is typeof Parent making it implicitly typeof IMyInterface
        
    public function myMethod()
        {
            print 
    __METHOD__;
        }
    }

    function 
    execMyMethod(IMyInterface $imiObj)
    {
        
    $imiObj->myMethod();
    }

    $p = new Parent();
    $c = new Child();

    execMyMethod($p); // Prints Parent::myMethod
    execMyMethod($c); // Prints Child::myMethod 
    What this does is allows any child of Parent to dynamically override via erasure the myMethod to replace the desired functionality of Child into Parent. The execMyMethod would also work with a typeof Parent.

    Final simply indicates last in chain allowed, and cannot be overridden from that point on. Can be applied from either a class or method level, including static methods:
    PHP Code:
    class Parent
    {
        public function 
    myMethod(){}
    }
    class 
    Child extends Parent
    {
        public final function 
    myMethod(){}
    }
    class 
    Child2 extends Child
    {
        public function 
    myMethod(){} // Fatal error, cannot override final method declared in Child
    }

    final class 
    Child3 extends Parent
    {
    }
    class 
    Child4 extends Child3 // Fatal error, cannot override final class Chiild3
    {

    Does that make sense?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    johnnnn (07-14-2009)

  • #4
    New Coder
    Join Date
    May 2009
    Location
    Pennsylvania, United States
    Posts
    54
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Thanks

    I thanked both of you, and I have a better understanding now.


  •  

    Tags for this Thread

    Posting Permissions

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