DarkMantis
DarkMantis

Reputation: 1516

Extending a PHP class

Just a quick question really.

I am re-writing a site and I am converting it all to OOP and putting it all into templates.

I have multiple classes, but I want to extend one of them from a separate file/class.

Do I have to require_once() the parent class to extend it?

I'm guessing you do but I just wanted to make sure.

Thanks in advance!

Upvotes: 1

Views: 140

Answers (4)

Johan Dettmar
Johan Dettmar

Reputation: 29426

Nomally it's neat to define an autoloader when you want to include a bunch of classes from a bunch of different files such as:

function __autoload( $className ) {
    $className = str_replace( "..", "", $className );
    require_once( "classes/$className.php" );
    // echo "Loaded classes/$className.php";
}

And then load whatever class name you have, i.e.:

$member = new Member();

Upvotes: 2

user680786
user680786

Reputation:

Of course NO.
Don't write code in PHP4-style.
Use autoloading, namespaces and naming standards: PSR-0

Upvotes: 4

cypher
cypher

Reputation: 6992

Yes, you have, otherwise the class wouldn't "exist" to PHP. Also, if you have some sensible naming convention, you should have a look into autoloading.

Upvotes: 2

Joe
Joe

Reputation: 15802

Yes, or you can look at http://www.php.net/spl_autoload_register to write a simple autoloader.

Upvotes: 5

Related Questions