tags:

views:

84

answers:

5

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?

+1  A: 

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
} 
bestattendance
Thanks, what a moron I've been.
PHP Newbie
+2  A: 

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 {
   ....
}
Kazar
Thanks, what a moron I've been. ;)
PHP Newbie
+1  A: 

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

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

Then you can extend it!

Enrico Carlesso
Thanks, what a moron I've been.
PHP Newbie
Everyone needs to learn! By the way, you should read about the difference between `include()`, `require()` and `require_once()`!include() will throw a non-fatal error if php file missing, require() act the same way but with a fatal error, and require_once avoids the php file to be recalled multiple times from different scripts! Can check here => http://it.php.net/require_once
Enrico Carlesso
+6  A: 

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...)

Pascal MARTIN
Thx for the response. I'm on PHP 5.2.8 (see my reply above).
PHP Newbie
OK about PHP 5.2 vs PHP 4 : that's good news :-)
Pascal MARTIN
A: 

I would use namaspaces

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

class MyController extends package_products_multicontroller\MultiActionController {
    function __construct() {
        parent::__construct();
    }
}
streetparade
This seems to be the clearest (read: 'most unambiguous') way (although a bit verbose, if I may dare say that).
PHP Newbie