PHP Newbie
PHP Newbie

Reputation: 61

Extending classes in 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?

Upvotes: 6

Views: 3684

Answers (5)

streetparade
streetparade

Reputation: 32878

I would use namaspaces

namespace package_products_multicontroller {
include("/packages/products/multicontroller.php");
}

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

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

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][1], to avoid having to write an enormous amount of [`require`][2] 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...)

Upvotes: 7

Enrico Carlesso
Enrico Carlesso

Reputation: 6934

You have to include the file holding the class, with a banal include() statement:

include('lib/controllers/MultiAction.php');

Then you can extend it!

Upvotes: 1

Alistair Evans
Alistair Evans

Reputation: 36473

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 {
   ....
}

Upvotes: 2

bestattendance
bestattendance

Reputation: 26902

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
} 

Upvotes: 1

Related Questions