note 90240 deleted from language.oop5.paamayim-nekudotayim by googleguy

From: Date: Thu, 18 Feb 2016 04:11:27 +0000
Subject: note 90240 deleted from language.oop5.paamayim-nekudotayim by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-204973@lists.php.net to get a copy of this message
Note Submitter: erwinhaantjes at interurbia dot nl ---- Here is a nice function i like to share with you because it so nice and handy feature. I wanted to get rid off the parent::methodName(myVar1, myVar2, myVar3 etc.....) construction so i wrote a shortcut to this notation. The only thing you have to do is calling $this->inherited() (or parent::inherited()) to call the same function with the same parameters in the parent class. It is also possible to specify other parameters if you like. You can also use this inside constructors/destructors. To use this example, put it in the base class of all objects.You also need the simple function below to test if an array is valid (from a self-build library) but you are free to change it to your own needs ;-): <?php function suIsValidArray( &$a, &$iCount = null ) { $iCount = 0; return ( is_array( $a ) and (( $iCount = @count( $a )) > 0 )); } ?> The inherited function/construction is familiar language construction in Object Pascal/Delphi (wonderful language). If you have any comments, please enter a note. Here it is: <?php public function inherited() { // Use DEBUG backtrace to trace caller function $bt = debug_backtrace(); $bt = $bt[ 1 ]; // List is in reversed order, 0 reffers to this function so get previous one if( !suIsValidArray( $bt )) { return; } $sClassName = $bt["class"]; $sParentClassName = get_parent_class( $sClassName ); if( empty( $sClassName ) or empty( $sParentClassName ) or $sParentClassName == $sClassName ) { return; } $sFuncName = $bt["function"]; // constructor or destructor called (old fashion way)? if( ( $bIsConstruct = ( $sFuncName == $sClassName )) or ( $sFuncName == "_".$sClassName ) ) { // get parent constructor/destructor $sFuncName = (( !$bIsConstruct ) ? "_" : "" ).$sParentClassName; if( $sFuncName == (( !$bIsConstruct ) ? "_" : "" ).$sClassName ) { return; } } if( method_exists( $sParentClassName, $sFuncName )) { // If there are parameters specified, use these $args = func_get_args(); if( !suIsValidArray( $args )) { $args = &$bt["args"]; } // otherwise use previous function parameters if any $iCount = 0; $sArgs = ""; if( suIsValidArray( $args, $iCount )) { for( $i = 0; $i < $iCount; $i++ ) { $sArgs.="&$"."args[$i]".( $i < ($iCount-1) ? "," : "" ); } } // Simple, evaluate it, this is possible because it is done inside the class itself, and // the parent class is already created because is a part of the class structure, no worry, no scope issues eval( "$"."result = $sParentClassName::".$sFuncName."($sArgs);" ); return @$result; } } ?>

« previous php.notes (#204973) next »