Jump to content

Archived

This topic is now archived and is closed to further replies.

ignace

OOP - Some questions regarding final, interface, implements, extends

Recommended Posts

Hey after some googling and reading, have i come to a certain page in my life, that i got stuck! I'm a huge fan of normalisation (it makes atleast my life a bit easier) so now am i trying to do this also on some oop (object oriented programming - for those who do not know what i mean)

So if interface, implements, extends, etc... is no foreign language for you, then please feel free to be so kind and enlight my mind... xD

Thanx in advance,
Ignace

Share this post


Link to post
Share on other sites
This may help with some of the questions.

http://www.phpfreaks.com/forums/index.php/topic,104878.0.html

An interface is similar to a parent class except it has no code defined in it, just a list of method declarations which the implementing class must define.

Share this post


Link to post
Share on other sites
an interface provides rules for an objects relational accessibility (i.e. interface)

Much like USB has a certain shape to it's plugs, a class interface defines the access points.

Abstract classes are simply parent classes that must be extended in order to use them. You can specify abstract methods that must be overloaded by the child class, whilst also maintaining common functionality amongst those child classes within the parent abstract class.

Classes can only extend one parent, but can implement many interfaces.

Final is the opposite of abstract. It cannot be overloaded/extended.

Share this post


Link to post
Share on other sites
thx for the information, @barand so an interface is much like the prototypes that you can define in c++? but now you use a child class to implement them instead of adding them after [ int main(); ]

Share this post


Link to post
Share on other sites
Slightly different emphasis. Prototypes tell you what is defined, interface tells what [b]must be[/b] defined for the class to function correctly

Share this post


Link to post
Share on other sites
k, thx, now something else, overloading is this done in the same way as in c++?

string doMyHomework(string Input);
string doMyHomework(double Input);

however i can not define the return type or the argument type

function doMyHomework($Input);
function doMyHomework($Input); // cannot redeclare function 'doMyHomework'

so how do i overload a php function?

Share this post


Link to post
Share on other sites
As php isn't a strongly typed language like C it doesn't really apply.
[code]<?php
function doubleMe ($input) {
    return $input * 2;
}

$x = "2";        // string
$y = 2.75;      // float
$z = 3;          // int

echo doubleMe($x);
echo '<br/>';
echo doubleMe($y);
echo '<br/>';
echo doubleMe($z);
?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.