开发者

Extending a PHP class

开发者 https://www.devze.com 2023-04-03 13:34 出处:网络
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.

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 h开发者_如何学JAVAave 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!


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


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


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消