Skip to content
This repository has been archived by the owner on Feb 27, 2020. It is now read-only.

PHP4 style constructors are deprecated in PHP7 #18

Merged
merged 2 commits into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions classes/tests/critical.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class critical {
'reservedNames',
'deprecatedFunctions',
'newOperatorWithReference',
'oldClassConstructors',
];

/**
Expand Down Expand Up @@ -133,5 +134,30 @@ public function _newOperatorWithReference($line) {
}
return false;
}

public function _oldClassConstructors($line) {
static $lastClassName = false;

// reset the name of the class that we've seen
if ($line === '<?php') {
$lastClassName = false;
}

// find the start of PHP class declaration
if (strpos($line, 'class') === 0) {
if (preg_match('#class (\w+)#', $line, $matches)) {
$lastClassName = $matches[1];
}
}

// is the class name used as the function name?
if ($lastClassName !== false && strpos($line, 'function') !== false) {
if (preg_match("#function {$lastClassName}\s?\(#", $line)) {
return true;
}
}

return false;
}
}
?>
11 changes: 11 additions & 0 deletions testcases.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,14 @@ trait numeric { /*...*/ }
class C {}
$c =& new C;
$c =&new C;

// Methods with the same name as their class will not be constructors in a future version of PHP
class FooBar {
var $test = 42;

function set() {}

function FooBar() {
// NOP
}
}