note 88249 added to language.oop5.basic

From: Date: Sat, 17 Jan 2009 04:37:22 +0000
Subject: note 88249 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-149360@lists.php.net to get a copy of this message
A simple approach to Multiple Inheritance You can give yourself something approaching multiple inheritance with the following class: <?php class inheritance{ var $bases = array(); static function error_die( $errno, $errstr, $errfile, $errline ) { $backtrace = debug_backtrace(); $detail = $backtrace[4]; var_dump( $backtrace ); echo '<b>Fatal Error</b>: '.$errstr.' of class <b>'.$detail["class"].'</b> in <b>'.$detail["file"].'</b> on line <b>'.$detail["line"].'</b><br/>'; die(); } private function fatal( $text ) { set_error_handler( array( 'inheritance', 'error_die' ) ); trigger_error( $text, E_USER_ERROR ); restore_error_handler(); } function __call( $name, $args ) { if( $this->bases ) foreach( $this->bases as $base ) if( method_exists( $base, $name ) ) return $base->$name( $args ); $this->fatal( "Call to undefined method <b>".$name."</b>" ); } function __set( $name, $value ) { if( $this->bases ) foreach( $this->bases as $base ) if( property_exists( $base, $name ) ) { $base->$name = $value; return; } } function __get( $name ) { if( $this->bases ) foreach( $this->bases as $base ) if( property_exists( $base, $name ) ) return $base->$name; } function __isset( $name ) { if( $this->bases ) foreach( $this->bases as $base ) if( property_exists( $base, $name ) ) return isset( $base->$name ); } function __unset( $name ) { if( $this->bases ) foreach( $this->bases as $base ) if( property_exists( $base, $name ) ) { unset( $base->$name ); return; } } function inherits( $name, $args = '' ) { return array_unshift( $this->bases, new $name( $args ) ); } } ?> Most of the qualities of multiple inheritance provided by this class are revealed by the following code: <?php //test inheritance class base0 { public $base0var; public $basevar; function base0declare() { echo 'I am base 0'; } function basedeclare() { self::base0declare() { } } class base1 extends base0 { // simple linear inheritance here public $base1var; public $basevar; function based1declare() { echo 'I am base 1'; } function basedeclare() { self::base1declare() } } class base2 public $base2var; public $basevar; function based2declare() { echo 'I am base 2'; } function basedeclare() { self::base2declare } } ?> Multiple inheritance is achieved by extending the inheritance class, and then in the __construct function placing calls to the "inherits" method of the inheritance class. Each call pushes an instance of the inherited class into an array var which functions as a LIFO stack. Using the magic methods, any failed method call, property set, get, isset or unset is intercepted by the inheritance base class which then attempts to resolve the reference. Object method name conflicts are resolved simply by the later inheritance masking the scope of the earlier inherited method. I recognize there are shortcomings to the approach I offer here, but it works for all my current multiple inheritance needs and offers simplicity and ease of understanding as benefits. <?php class base_test extends inheritance { // multiple inheritance function __construct() { $this->inherits( 'base1' ); $this->inherits( 'base2' ); } } ?> Here are some code fragments you can try out to test things. <?php $testobj = new base_test(); var_dump( $testobj ); $testobj->base2declare(); $testobj->base1declare(); $testobj->base0declare(); $testobj->basedeclare(); $testobj->base2var = 27; echo $testobj->base2var; ?> I'd be interested in hearing any comments. ---- Server IP: 69.147.83.197 Probable Submitter: 208.65.90.205 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/88249 Del: integrated -- https://master.php.net/note/delete/88249/integrated Del: useless -- https://master.php.net/note/delete/88249/useless Del: bad code -- https://master.php.net/note/delete/88249/bad+code Del: spam -- https://master.php.net/note/delete/88249/spam Del: non-english -- https://master.php.net/note/delete/88249/non-english Del: in docs -- https://master.php.net/note/delete/88249/in+docs Del: other reasons-- https://master.php.net/note/delete/88249 Reject -- https://master.php.net/note/reject/88249 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#149360) next »