Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP7 Constructor class name

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 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: ...)

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.

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.

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

Thanks!

like image 354
Thomas M. Avatar asked Mar 31 '16 17:03

Thomas M.


People also ask

How do we define a constructor when class name?

Answer: We define a constructor as x() ,when class name is x. Explanation: Constructor in Object Oriented Programming is a special method that is invoked automatically at the time an object is created. It is used to initialize the data members of new objects .

What are the __ construct () and __ destruct () methods in a PHP class?

Example# __construct() is the most common magic method in PHP, because it is used to set up a class when it is initialized. The opposite of the __construct() method is the __destruct() method. This method is called when there are no more references to an object that you created or when you force its deletion.

How do you call a parent constructor?

In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private). $obj = new OtherSubClass();

What is __ construct in PHP?

PHP - The __construct FunctionA constructor allows you to initialize an object's properties upon creation of the object. If you create a __construct() function, PHP will automatically call this function when you create an object from a class. Notice that the construct function starts with two underscores (__)!


1 Answers

As I understand this, in PHP4 my code was buggy, but not in PHP7, right?

Not quite. PHP4-style constructors still work on PHP7, they are just been deprecated and they will trigger a Deprecated warning.

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

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.

like image 107
Federkun Avatar answered Oct 01 '22 20:10

Federkun