note 90240 added to language.oop5.paamayim-nekudotayim
| From: | erwinhaantjes at interurbia dot nl | Date: | Sat, 11 Apr 2009 21:36:39 +0000 |
| Subject: | note 90240 added to language.oop5.paamayim-nekudotayim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153098@lists.php.net to get a copy of this message | ||
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 ;-):
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:
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;
}
}
----
Server IP: 83.137.20.135
Probable Submitter: 83.86.169.215
----
Manual Page -- http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
Edit -- https://master.php.net/note/edit/90240
Del: integrated -- https://master.php.net/note/delete/90240/integrated
Del: useless -- https://master.php.net/note/delete/90240/useless
Del: bad code -- https://master.php.net/note/delete/90240/bad+code
Del: spam -- https://master.php.net/note/delete/90240/spam
Del: non-english -- https://master.php.net/note/delete/90240/non-english
Del: in docs -- https://master.php.net/note/delete/90240/in+docs
Del: other reasons-- https://master.php.net/note/delete/90240
Reject -- https://master.php.net/note/reject/90240
Search -- https://master.php.net/manage/user-notes.php