PHP7 构造函数类名

2023-06-22php开发问题
8

本文介绍了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 构造函数类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17