__construct和与类名同名的函数有什么区别? [英] What's difference between __construct and function with same name as class has?

查看:269
本文介绍了__construct和与类名同名的函数有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
what is the function __construct used for?

在__construct函数和与类名相同的函数之间有什么区别吗?

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


推荐答案

命名的方法是PHP4方法

The method named is the PHP4 way of doing a constructor.


为了向后兼容,如果PHP 5找不到给定类的__construct()函数,它将搜索旧式构造函数,按类名。有效地,这意味着,唯一的情况下,会有兼容性问题是如果类有一个名为__construct()的方法用于不同的语义。

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.

As PHP 5.3.3中,与命名空间类名称的最后一个元素同名的方法将不再被视为构造函数。此更改不会影响非命名空间类。

As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.

http:// www。 php.net/manual/en/language.oop5.decon.php

这篇关于__construct和与类名同名的函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆