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

Commit

Permalink
Merge pull request #18 from macbre/oldClassConstructors
Browse files Browse the repository at this point in the history
PHP4 style constructors are deprecated in PHP7
  • Loading branch information
Alexia committed Apr 26, 2016
2 parents b0d4786 + b3f890f commit 1fa5638
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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
}
}

0 comments on commit 1fa5638

Please sign in to comment.