Skip to content

Commit 1fa5638

Browse files
committed
Merge pull request Alexia#18 from macbre/oldClassConstructors
PHP4 style constructors are deprecated in PHP7
2 parents b0d4786 + b3f890f commit 1fa5638

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

classes/tests/critical.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class critical {
2929
'reservedNames',
3030
'deprecatedFunctions',
3131
'newOperatorWithReference',
32+
'oldClassConstructors',
3233
];
3334

3435
/**
@@ -133,5 +134,30 @@ public function _newOperatorWithReference($line) {
133134
}
134135
return false;
135136
}
137+
138+
public function _oldClassConstructors($line) {
139+
static $lastClassName = false;
140+
141+
// reset the name of the class that we've seen
142+
if ($line === '<?php') {
143+
$lastClassName = false;
144+
}
145+
146+
// find the start of PHP class declaration
147+
if (strpos($line, 'class') === 0) {
148+
if (preg_match('#class (\w+)#', $line, $matches)) {
149+
$lastClassName = $matches[1];
150+
}
151+
}
152+
153+
// is the class name used as the function name?
154+
if ($lastClassName !== false && strpos($line, 'function') !== false) {
155+
if (preg_match("#function {$lastClassName}\s?\(#", $line)) {
156+
return true;
157+
}
158+
}
159+
160+
return false;
161+
}
136162
}
137163
?>

testcases.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,14 @@ trait numeric { /*...*/ }
127127
class C {}
128128
$c =& new C;
129129
$c =&new C;
130+
131+
// Methods with the same name as their class will not be constructors in a future version of PHP
132+
class FooBar {
133+
var $test = 42;
134+
135+
function set() {}
136+
137+
function FooBar() {
138+
// NOP
139+
}
140+
}

0 commit comments

Comments
 (0)