PHP OOP - Require or Extend
I was wondering if there is any major different in the following, and whether one is more 'standard' than the other:
<?php
class Account extends Database {
public fun开发者_JAVA百科ction myMethod()
{
// Do something
}
}
?>
or
<?php
require('database.class.php');
class Account {
public function myMethod()
{
// Do something
}
}
?>
Cheers :)
Edit:
This question actually relates to a tutorial series I have been following which describes the above two methods - which didn't make any clear sense.
So thank you for the constructive answers on clearing that one up!
Those are two completely separate language constructs.
Your first example deals with inheritance. Basically, you already have a class called Database
, but you want to have a specialized version of that class to handle accounts. Rather than build a brand new Account
class and copy/paste all the functionality you already have in your Database
class, you simply tell PHP that you want to use the existing Database
class as a baseline. You create any account-specific functionality in the new Account
class, and anything database-related comes automatically. This is assuming, of course, that you have some way of specifying where the Database
class is defined - for example, a require
declaration at the top of the class, or an __autoload()
or spl_autoload_register()
function call defining a way to find and locate the file containing the Database
class.
In your second example, your database-related code is completely separated from your Account
class. They're completely distinct entities, and if you wanted to do anything database-related in your Account
class, you would have to explicitly instantiate a new Database
object within that class (or pass it to that class, or one of its functions, as a parameter.
Basically, extends
helps define what a class is, whereas require
shows where a class definition (or other code) is stored.
Both code snippets aren't even equivalent.
The first declares Account to extend Database, a is-a relation.
In the second code snippet, you are simply saying that you require 'database.class.php' ... and that neither has anything to do with OO, nor defines a is-relation from Account to Database.
Both are completely different in first one class is inherited by another class but in the second one the class is included in your script only.
Means if you extend
all the public and protected methods are available in your derived class and you can create object of derived class and can use methods with derived class's object.
But in the second method the class is included in your script and require this class it's own method and work independently.
The first means you create a new class, which has all the functionality of Database class and those you implement.
The second means that you create a new class, but it doesn't have Database functionality since it's not extending it. If you need database access in your Account class, you can create an instance in constructor, or pass already created instance as constructor parameter.
It's hard to say what is more standard, since it depends on what You actually want to achieve.
To put it in most simple terms:-
require
or include
is structural programming.
extends
is object oriented
精彩评论