PHP7 构造函数类名 [英] PHP7 Constructor class name

查看:44
本文介绍了PHP7 构造函数类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Laravel 4.2 应用程序,它可以与 PHP5 一起使用,没有任何问题.由于我安装了一个运行 PHP7 的新 vagrant 框,因此只要我运行一个模型,其中函数名称与类名称(关系函数)相同,就会出现错误,如下所示:

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:

<?php

use IlluminateDatabaseEloquentSoftDeletingTrait;

class Participant extends Eloquent
{

    use SoftDeletingTrait;

    [...]

    public function participant()
    {
        return $this->morphTo();
    }

    [...]    

}

我收到以下错误消息:

与它们的类同名的方法在 PHP 的未来版本中将不再是构造函数;参与者有一个已弃用的构造函数(查看:...)

Methods with the same name as their class will not be constructors in a future version of PHP; Participant has a deprecated constructor (View: ...)

所以直到今天我才知道,在 PHP4 中,同名的方法是类的构造函数.唔.我真的是一个糟糕的程序员......但在这种情况下,根据我对 PHP7 中发生的事情的理解,他们纠正了我的失败,因为我从来不想使用这个函数作为构造函数,因为它只定义了一个 Eloquent 关系.

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 中没有,对吗?如果没有必要,我不想重构这个函数,因为它在几个地方使用.

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.

谁能解释一下我做错了什么以及为什么它适用于较旧的 PHP 版本?

Can anybody explain what I am doing wrong and why it worked with older PHP versions?

谢谢!

推荐答案

据我所知,在 PHP4 中我的代码有问题,但在 PHP7 中没有,对吧?

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.

你可以做的是定义一个 __construct 方法,甚至是一个空的方法,这样 php4-constructor 方法就不会在新创建的类实例上被调用.

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.

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

它适用于较旧的 PHP 版本,因为构造函数没有返回值.每次创建 Participant 实例时,都会隐式调用 participant 方法,仅此而已.

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.

这篇关于PHP7 构造函数类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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