Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending classes in PHP

Tags:

php

When extending classes in Java, class name ambiguity is avoided by the usage of qualified package names in the import statements.

For example: Say I want my controller to extend Spring's MultiActionController - I'll import the same from the standard Spring package. This also prevents me from extending Mike's or Perry's MultiActionController, since I have not imported MultiActionController from their packages.

Similarly in PHP, say we have 10 classes in 10 different library folders, all of which are called MultiActionController.

When I write:

class MyController extends MultiActionController {
    function __construct() {
        parent::__construct();
    }
}

How do I tell PHP which MultiActionController (from which folder), to extend from?

like image 550
PHP Newbie Avatar asked Feb 28 '10 15:02

PHP Newbie


3 Answers

Having several classes with the same name will, one day or another, cause some problems : you cannot include two classes with the same name during the execution of one script -- It'll get you a Fatal Error.

What's generally done, in PHP (before PHP 5.3 and namespaces, at least) is to include the name of the library and/or the "package" in the class name.

For instance, you could have a class name MyLibrary_Package_MultiActionController, and another called OtherLib_Blah_MultiActionController.


Then, what's generally done is using that class name to "map" it to directories and files, replacing the '_' by '/', and adding .php at the end of the last level -- this being often done using the autoloading feature of PHP, to avoid having to write an enormous amount of require directives.

For instance, a class named MyLibrary_Package_MultiActionController should be stored in MyLibrary/Package/MultiActionController.php.


As a sidenote : you used the tag "php4", in your question... If you are actually using PHP 4, you should not forget that it's old, not maintained anymore (Not even for security-related problems), and that PHP 5 is really the way to go !

In fact, you won't be able to do much about object-oriented programming, with PHP 4 ; the object-oriented stuff in PHP 4 was really basic...

(Stuff such as autoloading, which I wrote about a couple of paragraph earlier didn't exists in PHP 4 -- same for public/private/protected, and lots of other OO-related things...)

like image 110
Pascal MARTIN Avatar answered Oct 17 '22 21:10

Pascal MARTIN


It depends which one you include. PHP will not let you redefine a class of the same name, so just include above the class definition (change to fit the file names and your software layout):

include('../includes/Spring/MultiActionController.php');

class MyController extends MultiActionController {
   ....
}
like image 2
Alistair Evans Avatar answered Oct 17 '22 22:10

Alistair Evans


PHP will extend the class that you included with an include statement.

For example, say that you have a class foo declared in file bar.php:

class Foo {
    // methods and fields
}

Then in another fie:

include 'bar.php';
class Aardvark extends Foo {
    // this class will extend the class Foo in file bar.php
} 
like image 1
bestattendance Avatar answered Oct 17 '22 20:10

bestattendance