<bdo id='LssRL'></bdo><ul id='LssRL'></ul>
  1. <i id='LssRL'><tr id='LssRL'><dt id='LssRL'><q id='LssRL'><span id='LssRL'><b id='LssRL'><form id='LssRL'><ins id='LssRL'></ins><ul id='LssRL'></ul><sub id='LssRL'></sub></form><legend id='LssRL'></legend><bdo id='LssRL'><pre id='LssRL'><center id='LssRL'></center></pre></bdo></b><th id='LssRL'></th></span></q></dt></tr></i><div id='LssRL'><tfoot id='LssRL'></tfoot><dl id='LssRL'><fieldset id='LssRL'></fieldset></dl></div>
  2. <small id='LssRL'></small><noframes id='LssRL'>

      <tfoot id='LssRL'></tfoot>
    1. <legend id='LssRL'><style id='LssRL'><dir id='LssRL'><q id='LssRL'></q></dir></style></legend>

      PHP7 构造函数类名

      时间:2023-06-22
        <tbody id='2CpS8'></tbody>
        <i id='2CpS8'><tr id='2CpS8'><dt id='2CpS8'><q id='2CpS8'><span id='2CpS8'><b id='2CpS8'><form id='2CpS8'><ins id='2CpS8'></ins><ul id='2CpS8'></ul><sub id='2CpS8'></sub></form><legend id='2CpS8'></legend><bdo id='2CpS8'><pre id='2CpS8'><center id='2CpS8'></center></pre></bdo></b><th id='2CpS8'></th></span></q></dt></tr></i><div id='2CpS8'><tfoot id='2CpS8'></tfoot><dl id='2CpS8'><fieldset id='2CpS8'></fieldset></dl></div>
        1. <small id='2CpS8'></small><noframes id='2CpS8'>

              <bdo id='2CpS8'></bdo><ul id='2CpS8'></ul>
              <tfoot id='2CpS8'></tfoot><legend id='2CpS8'><style id='2CpS8'><dir id='2CpS8'><q id='2CpS8'></q></dir></style></legend>

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

                上一篇:在 PHP5 类中,何时调用私有构造函数? 下一篇:为什么 PHP 允许“不兼容"构造函数?

                相关文章

              • <tfoot id='bEtxP'></tfoot>

                      <bdo id='bEtxP'></bdo><ul id='bEtxP'></ul>
                    <i id='bEtxP'><tr id='bEtxP'><dt id='bEtxP'><q id='bEtxP'><span id='bEtxP'><b id='bEtxP'><form id='bEtxP'><ins id='bEtxP'></ins><ul id='bEtxP'></ul><sub id='bEtxP'></sub></form><legend id='bEtxP'></legend><bdo id='bEtxP'><pre id='bEtxP'><center id='bEtxP'></center></pre></bdo></b><th id='bEtxP'></th></span></q></dt></tr></i><div id='bEtxP'><tfoot id='bEtxP'></tfoot><dl id='bEtxP'><fieldset id='bEtxP'></fieldset></dl></div>

                    <small id='bEtxP'></small><noframes id='bEtxP'>

                    <legend id='bEtxP'><style id='bEtxP'><dir id='bEtxP'><q id='bEtxP'></q></dir></style></legend>