note 77392 added to language.oop5
| From: | AouieWeb_Form_Aouie dot net at osu1 dot php dot net | Date: | Tue, 28 Aug 2007 03:02:30 +0000 |
| Subject: | note 77392 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129843@lists.php.net to get a copy of this message | ||
(Repeating the post made under classes for PHP 4 section.)
class casting (type casting to an extended class) workaround. Useful to use functionality in an
extended class on an instance of a base class (especially when the BaseClass file needs to be small
and the extended class functions are huge). Tested to work on public, protected and private vars in
PHP5.
---
class ABaseClass
{
private $T1;
protected $T2;
public $T3;
public function CopyFrom( )
{
// If this class had parents then you will want to implement and use parent::$CopyFrom( ). If
not, then private members in the parent class will not be copied.
return get_object_vars( $this );
}
protected function CopyInto( $VarArr )
{
// If this class had parents then in order to copy the private values we must implement and use
parent::CopyInto( $VarArr ).
$TempArr = get_class_vars( __CLASS__ );
foreach( $TempArr as $VarName => $Var1 )
{
$this->$VarName = $VarArr[ $VarName ];
}
}
}
class AnExtendedClass extends ABaseClass
{
public function __construct( $theBaseClassInstance )
{
$this->CopyInto( $theBaseClassInstance->CopyFrom( ) );
}
public function LongFunctionIntheFileWithTheExtendedClass( )
{
// Bla Bla Bla
}
}
---
$BaseInstance = new ABaseClass();
...
$ExtendedInstance = new AnExtendedClass( $BaseInstance );
$ExtendedInstance->LongFunctionIntheFileWithTheExtendedClass( );
----
Server IP: 69.147.83.197
Probable Submitter: 76.208.154.218
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- https://master.php.net/note/edit/77392
Del: integrated -- https://master.php.net/note/delete/77392/integrated
Del: useless -- https://master.php.net/note/delete/77392/useless
Del: bad code -- https://master.php.net/note/delete/77392/bad+code
Del: spam -- https://master.php.net/note/delete/77392/spam
Del: non-english -- https://master.php.net/note/delete/77392/non-english
Del: in docs -- https://master.php.net/note/delete/77392/in+docs
Del: other reasons-- https://master.php.net/note/delete/77392
Reject -- https://master.php.net/note/reject/77392
Search -- https://master.php.net/manage/user-notes.php