PHP7 Constructor class name
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.
How to make PHP version 8 support constructor with same name as class?
This isn't a case of a setting that you can turn back on, I'm afraid, the functionality was permanently removed from PHP. It might be possible to write some kind of extension which emulated the old behaviour, but it's going to be a lot more work in the long run than doing a one-off fix to all your existing files.
Your best bet is probably to use a tool such as Rector which can automate the upgrade process. In this case, using the Php4ConstructorRector rule looks like it should do it all for you.
__construct() vs SameAsClassName() for constructor in PHP
I agree with gizmo, the advantage is so you don't have to rename it if you rename your class. DRY.
Similarly, if you have a child class you can call
parent::__construct()
to call the parent constructor. If further down the track you change the class the child class inherits from, you don't have to change the construct call to the parent.
It seems like a small thing, but missing changing the constructor call name to your parents classes could create subtle (and not so subtle) bugs.
For example, if you inserted a class into your heirachy, but forgot to change the constructor calls, you could started calling constructors of grandparents instead of parents. This could often cause undesirable results which might be difficult to notice.
Also note that
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.
Source: http://php.net/manual/en/language.oop5.decon.php
php 7 deprecated constructor issue
In a very old version of PHP (PHP 4, to be precise), having a function name with the same name as the class was used as the constructor.
That is, if you were to call new my_function()
, the method my_function
inside the class would be called with any parameters.
Now, if you're using the my_function
as a constructor, a drop-in replacement would be to rename it to __construct
. That will work whenever an instance is initiated.
If, on the other hand, you just happen to have a function that shares a name with the class, and are not relying on it as a constructor, you should be able to ignore that warning altogether, as it's referencing a deprecated functionality that you're not using.
Edit: If it's the second scenario, make sure to also add a public function __construct()
, if there's not one there already. Leaving it empty is fine, but just having it will make sure PHP doesn't inadvertently try to call my_function
on instantiation, as the __construct
will take precedence.
Example:
<?php
class Foo
{
//DEPRECATED CONSTRUCTOR SYNTAX (Don't try this at home.)
public function Foo()
{
echo "Foo named constructor\n";
}
}
$f = new Foo();
//Foo named constructor
class Bar
{
public function __construct()
{
echo "Bar constructor\n";
}
public function Bar()
{
echo "Bar function\n";
}
}
$b = new Bar();
//Bar constructor
$b->Bar();
//Bar function
Migrating PHP 7 to 8 - Object's constructor is not called
Propably incompatible changes.
From docs:
Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.
Source: https://www.php.net/manual/en/migration80.incompatible.php
Use of deprecated PHP4 style class constructor is not supported since PHP 7
Change the function to:
function __construct($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
As you used to be able to define constructors via the class name and that has been deprecated as of PHP 7:
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.
The error example, as per the documentation:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3
Regex to replace function name in PHP classes
As said in the comments, better use a scripting language, some logic and a two-factor-approach:
- Match the class including the class name as "a whole" using a recursive approach
- Replace the function with the class name with
__construct
In
PHP
:<?php
$class_regex = '~
^\s*class\s+
(?P<class>\S+)[^{}]+(\{
(?:[^{}]*|(?2))*
\})~mx';
$data = preg_replace_callback($class_regex,
function($match) {
$function_regex = '~function\s+\K'.$match['class'].'~';
return preg_replace($function_regex, '__construct', $match[0]);
},
$data);
echo $data;
?>
See a demo for the outer regex on regex101.com and for the whole code on ideone.com.
Related Topics
Laravel Error: Method Illuminate\View\View::_Tostring() Must Not Throw an Exception
Search and Replace Value in PHP Array
How to Specify the Parent Query Field from Within a Subquery in MySQL
Concat Columns with Laravel 5 Eloquent
Split an Array into N Arrays - PHP
Mysql_Connect VS MySQL_Pconnect
Navigate Back with PHP Form Submission
How to Capture Search Term from Google Search
JSON: PHP to JavaScript Safe or Not
Phpmailer - How to Remove Recipients
Jquery Load() and PHP Variables
Best Way to Parse an Invalid HTML in PHP
PHP Check If Time Is Between Two Times Regardless of Date
How to Enable Ssl in Wamp Server
But These Conflict with Your Requirements or Minimum-Stability