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 07-13-2009, 04:20 PM   PM User | #1
johnnnn
New Coder

 
Join Date: May 2009
Location: Pennsylvania, United States
Posts: 54
Thanks: 16
Thanked 0 Times in 0 Posts
johnnnn is an unknown quantity at this point
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.
johnnnn is offline   Reply With Quote
Old 07-13-2009, 10:27 PM   PM User | #2
funnymoney
Regular Coder

 
funnymoney's Avatar
 
Join Date: Aug 2007
Posts: 364
Thanks: 17
Thanked 24 Times in 24 Posts
funnymoney is an unknown quantity at this point
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
__________________
PHP Idea Factory

Last edited by funnymoney; 07-13-2009 at 10:36 PM..
funnymoney is offline   Reply With Quote
Users who have thanked funnymoney for this post:
johnnnn (07-14-2009)
Old 07-13-2009, 11:42 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
johnnnn (07-14-2009)
Old 07-14-2009, 02:22 PM   PM User | #4
johnnnn
New Coder

 
Join Date: May 2009
Location: Pennsylvania, United States
Posts: 54
Thanks: 16
Thanked 0 Times in 0 Posts
johnnnn is an unknown quantity at this point
Thanks

I thanked both of you, and I have a better understanding now.
johnnnn is offline   Reply With Quote
Reply

Bookmarks

Tags
classes, final, method, object, oop

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 09:29 AM.


Advertisement
Log in to turn off these ads.