Created
August 2, 2012 11:36
-
-
Save e7o-de/3236439 to your computer and use it in GitHub Desktop.
PHP Multiple Inheritance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A { | |
function x() { echo 'Ich bin Klasse A'; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class B { | |
function y() { echo 'Ich bin Klasse B'; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class C extends A, B { | |
// Blubb | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function __autoload($class_name) { | |
if (!file_exists($class_name . '.php')) return; | |
$file = file_get_contents($class_name . '.php'); | |
$file = substr($file, 5); | |
preg_match_all('/class [[:alnum:]]+ extends [[:alnum:], ]+/i', $file, $classes, PREG_OFFSET_CAPTURE); | |
if (count($classes[0]) == 0) { | |
$file2 = $file; | |
} else { | |
$file2 = ''; | |
$lastPos = 0; | |
foreach ($classes as $class) { | |
if (!is_array($class) || count($class) == 0) continue; | |
$class = $class[0]; | |
$file2 .= substr($file, $lastPos, $class[1]); | |
$lastPos = $class[1] + strlen($class[0]); | |
$file2 .= substr($class[0], 0, strpos($class[0], ' extends')) . ' {'; | |
$extends = substr($class[0], strpos($class[0], ' extends') + 8); | |
$extends = explode(',', $extends); | |
foreach ($extends as $extend) { | |
$extend = trim($extend); | |
if (strlen($extend) < 1) continue; | |
if (!class_exists($extend)) { | |
__autoload($extend); | |
} | |
$includeFile = file_get_contents($extend . '.php'); | |
$includeFile = substr($includeFile, strpos($includeFile, '{') + 1); | |
$includeFile = substr($includeFile, 0, strrpos($includeFile, '}')); | |
$file2 .= $includeFile; | |
} | |
$file2 .= substr($file, $class[1] + strlen($class[0]) + 1); | |
} | |
} | |
eval($file2); | |
} | |
$c = new C(); | |
$c->x(); | |
$c->y(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Multiple inheritance is bad and you should feel bad :)