扩展类和将其包含在 PHP 中有什么区别?
之间有什么区别
include_once 'classb.php'
class A
{
$a = new B
}
有人可以向我解释一下和
class A extends B
{
$a = new B
}
吗?
扩展类与包含 .php 文件相比有哪些优点/缺点?
Can someone explain to me what the difference between
include_once 'classb.php'
class A
{
$a = new B
}
and
class A extends B
{
$a = new B
}
is?
What advantages/disadvantages are there to extending a class vs. including the .php file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
include_once
读取源文件,在本例中,该文件可能包含B
的类定义。 您的extends
将类A
设置为 继承 类B
,即A
获取B
中的所有内容,然后可以定义自己对该基本结构的修改。 这两个操作之间根本没有任何关系,并且您的$a = new B
操作是无意义的(更不用说语法错误)。Your
include_once
reads in a source file, which in this case presumably has a class definition forB
in it. Yourextends
sets up classA
as inheriting classB
, i.e.A
gets everything inB
and can then define its own modifications to that basic structure. There isn't really any relationship at all between the two operations, and your$a = new B
operations are nonsensical (not to mention syntax errors).如果您正在寻找更高级别的答案...
使用扩展将 A 绑定到执行与 B 类似的操作,而在 A 中创建 B 的实例不会对 A 施加任何限制。
第二种方法称为组合,是通常是构建面向对象系统的首选方法,因为它不会产生很高的类层次结构,并且从长远来看更灵活。 然而,它确实会产生稍慢的代码,并且比简单使用扩展的代码要多。
我的2美分
In case you're looking for a higher level answer...
Using extends binds A to doing things similarly to B, whereas creating an instance of B within A doesn't impose any restrictions on A.
The second approach is called Composition and is generally the preferred approach to building OO systems since it doesn't yield tall Class hierarchies and is more flexible long term. It does however yield slightly slower code, and more of it than then simple use of extends.
My 2 Cents
第二,即扩展,是类操作。
首先,即include - 是文件操作(在编译之前)。
所以 - 第二个永远不可能。 没有第一个。
second, ie extending, - is class operation.
First, ie include - is file operation, (before compile).
So - second is not possible ever. without first.
首先,include 是一个读取 php 源文件并将其视为代替 include 指令编写的命令。 你可以在任何php源文件上使用它,并且与类继承无关。
顺便说一句,如果您不包含包含 A 类源代码的文件,则无法扩展该类。
使用关键字extends扩展类意味着继承的概念,这是面向对象编程中的一个关键概念。
你得到一个带有一些方法的类(A),并且需要一个行为略有不同的类似类(B),你可以派生新类扩展类A并只修改必须更改的方法(称为覆盖)或添加新的。
与继承相反的是组合。 通过组合,您不必扩展类来改变其行为,而是必须对类进行拼凑才能获得所需的行为。
在 wikipedia 上,您可以找到关于对象组合和[类继承](http://en.wikipedia.org/wiki/Inheritance_(computer_science))
看来这些概念有些混乱,当你扩展类 A 的同时,你尝试组合它(顺便说一句,正确的语法是 $a=new B();)。
显然,这是一个有用的参考,[Gof,设计模式](http://en。 wikipedia.org/wiki/Design_Patterns_(book))
First of all, include is a command that read a php source file and treats it as it was written in place of the include directive. You can use it on any php source file and is not related to the class inheritance.
By the way, if you don't include the file containing the class A source code you can't extends that class.
Extendind a class with the keyword extends imply the concept of inheritance that is a key concept in object oriented programming.
You get a class (A) with some methods and need a similar class (B) with a slight different behaviour, you can derive the new class extendig the class A and modifying just the methods that have to change (it is called overriding) or adding new ones.
In opposite to the inheritance there is the composition. With the composition you don't extends a class to change its behavoiur instead you have to do a patchwork of classes in order to obtain the desired behaviour.
On wikipedia you can find a better explanation on object composition and [class inheritance](http://en.wikipedia.org/wiki/Inheritance_(computer_science))
It seems there is a little confusione on these concepts, as you extend your class A and, at the same time, you try to compose it (by the way, the correct syntax is $a=new B();).
A useful reference it, obviously, [Gof, Design patterns](http://en.wikipedia.org/wiki/Design_Patterns_(book))