__construct 和与类同名的函数有什么区别?
可能的重复:
函数 __construct 的用途是什么?
__construct 和 __construct 有什么区别函数和与类同名的函数?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
Possible Duplicate:
what is the function __construct used for?
is there any difference between __construct function and function with same name as class has?
class foo {
function foo ($something){
echo "I see ".$something." argument";
}
}
class bar {
function __construct ($something){
echo "<br />
I see ".$something." argument again";
}
}
$foo = new foo("foo");
$bar = new bar("bar");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
指定的方法是 PHP4 执行构造函数的方法。
http://www.php.net/manual/en/language.oop5 .decon.php
The method named is the PHP4 way of doing a constructor.
http://www.php.net/manual/en/language.oop5.decon.php
与类同名的构造函数是 PHP4 的向后兼容功能。自 PHP 5.3.3 起,它不适用于命名空间类。
如果
__construct
和class-named
函数都存在,则__construct
用作构造函数。Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3
If both
__construct
andclass-named
functions are present, then the__construct
is used as constructor.第一个是旧的 php4 风格“构造”。它与
__construct
基本相同。The first one is old php4 style "construct". It is basically the same as the
__construct
.不同之处在于,不推荐使用与类相同的名称调用构造函数。
The difference is that calling a constructor by the same name of the class is deprecated.
不同之处在于 PHP 5.3.3 及更高版本将把 function foo() 视为常规方法而不是构造函数。以前的版本会将其视为构造函数。
The difference is that PHP version 5.3.3 and above will treat
function foo()
as regular method and not constructor. Previous versions will treat it as a constructor.