php PHP7 构造函数类名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36339774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP7 Constructor class name
提问by Thomas M.
I have a Laravel 4.2 application which works with PHP5 without any problems. Since I installed a new vagrant box running PHP7 an error appears as soon as I run a model where the name of a function is the same as the class name (relationship-function) like this:
我有一个 Laravel 4.2 应用程序,它可以与 PHP5 一起使用,没有任何问题。由于我安装了一个运行 PHP7 的新 vagrant 框,因此只要我运行一个模型,其中函数名称与类名称(关系函数)相同,就会出现错误,如下所示:
<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Participant extends \Eloquent
{
use SoftDeletingTrait;
[...]
public function participant()
{
return $this->morphTo();
}
[...]
}
I get the following error message:
我收到以下错误消息:
Methods with the same name as their class will not be constructors in a future version of PHP; Participant has a deprecated constructor (View: ...)
与它们的类同名的方法在 PHP 的未来版本中将不再是构造函数;参与者有一个已弃用的构造函数(查看:...)
So what I didn't know until today is, that in PHP4 methods with the same name were the contructor of a class. Hmm. I am really a bad programmer... But in this case, from my understanding of what is happening in PHP7, they correct a failure of mine as I never wanted to use this function as a constructor, since it defines only an Eloquent relationship.
所以直到今天我才知道,在 PHP4 中,同名的方法是一个类的构造函数。唔。我真的是一个糟糕的程序员......但在这种情况下,根据我对 PHP7 中发生的事情的理解,他们纠正了我的失败,因为我从来不想使用这个函数作为构造函数,因为它只定义了一个 Eloquent 关系。
But how can I get rid of this message? As I understand this, in PHP4 my code was buggy, but not in PHP7, right? If not necessary I do not want to refactor this function, as it is used in several places.
但是我怎样才能摆脱这个消息呢?据我了解,在 PHP4 中我的代码有问题,但在 PHP7 中没有,对吗?如果没有必要,我不想重构这个函数,因为它在几个地方使用。
Can anybody explain what I am doing wrong and why it worked with older PHP versions?
任何人都可以解释我做错了什么以及为什么它适用于较旧的 PHP 版本?
Thanks!
谢谢!
回答by Federkun
As I understand this, in PHP4 my code was buggy, but not in PHP7, right?
据我了解,在 PHP4 中我的代码有问题,但在 PHP7 中没有,对吗?
Not quite. PHP4-style constructors still work on PHP7, they are just been deprecated and they will trigger a Deprecated warning.
不完全的。PHP4 风格的构造函数仍然适用于 PHP7,它们只是被弃用,它们将触发弃用警告。
What you can do is define a __construct
method, even an empty one, so that the php4-constructor method won't be called on a newly-created instance of the class.
你可以做的是定义一个__construct
方法,甚至是一个空的方法,这样 php4-constructor 方法就不会在新创建的类实例上被调用。
class foo
{
public function __construct()
{
// Constructor's functionality here, if you have any.
}
public function foo()
{
// PHP4-style constructor.
// This will NOT be invoked, unless a sub-class that extends `foo` calls it.
// In that case, call the new-style constructor to keep compatibility.
self::__construct();
}
}
new foo();
It worked with older PHP versions simply because constructors don't get return value. Every time you created a Participant instance, you implicitly call the participant
method, that's all.
它适用于较旧的 PHP 版本,因为构造函数没有返回值。每次创建 Participant 实例时,都会隐式调用该participant
方法,仅此而已。
回答by DevLoots
PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes that implement a __construct() method are unaffected.
PHP 4 样式的构造函数(与定义它们的类同名的方法)已被弃用,将来会被删除。如果 PHP 4 构造函数是类中定义的唯一构造函数,则 PHP 7 将发出 E_DEPRECATED。实现 __construct() 方法的类不受影响。
<?php
class foo {
function foo() {
echo 'I am the constructor';
}
}
?>
You can keep your old constructor but you need to add a new construct like that:
您可以保留旧的构造函数,但需要添加这样的新构造:
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Participant extends \Eloquent
{
use SoftDeletingTrait;
[...]
public function __construct()
{
return $this->morphTo();
}
public function participant()
{
return $this->morphTo();
}
[...]
}