PHP Child Class Accessing Parent Variable Problem

PHP Child Class Accessing Parent Variable Problem
<?php
echo “<br />”;
echo “<br />”;
?>
<?php
class aRzClsInnerClass
{
//
public $aRzVarA = NULL;
//
function __construct()
{
$this->aRzVarA = “Coder Rz Rasel”;
}
public function aRzFunB()
{
echo “Rz Rasel From Inner Class”;
}
//
}
class aRzClsOuterClass
{
//
public $aRzObjInstance = NULL;
//
function __construct()
{
/*if( !self::$aRzObjInstance )
{
self::$aRzObjInstance = new self();
}
*/
if( !$aRzObjInstance )
{
//$aRzObjInstance = new aRzClsInnerClass();
//echo “Dkl”;
}
$this->aRzObjInstance = new aRzClsInnerClass();
}
//
public static function aRzFunMakeAndGetClsObj()
{
/*if ( !self )
{
self::$aRzObjInstance = new self();
}*/
if( !$aRzObjInstance )
{
$aRzObjInstance = new aRzClsInnerClass();
//echo “Dkl”;
}
return new self();
}
//
public function aRzFun()
{
echo “Rz Rasel From Outer Class”;
}
//
}
?>
<?php
echo “<br />”;
echo “<br />————–<br />”;
$aRzObjOuterClass = aRzClsOuterClass::aRzFunMakeAndGetClsObj();
$aRzObjOuterClass->aRzObjInstance->aRzFunB();
echo “<br />”;
echo $aRzObjOuterClass->aRzObjInstance->aRzVarA;
echo “<br />————–<br />”;
echo “<br />”;
?>
<?php
echo “Rz Rasel Innder Outer Class”;
echo “<br />”;
$aRzObjOuterClass = new aRzClsOuterClass();
$aRzObjOuterClass->aRzObjInstance->aRzFunB();
echo “<br />”;
echo $aRzObjOuterClass->aRzObjInstance->aRzVarA;
echo “<br />”;
?>
<?php
class A
{
// These are the properties you want to access from the child object
public $property_a;
public $property_b;
public $property_c;
// This is the child object variable
public $child_object;
public function __construct( )
{
// Pass ‘this’ into the child so that the child has a reference back to the parent
$this->child_object = new B( $this );
echo get_parent_class( $this );
echo get_class( $this );
}
}

class B
{
// Holds a reference to the parent object
protected $parent_object;
public function __construct( $object )
{
// Remember the reference to the parent object
$this->parent_object = $object;
echo get_parent_class( $this );
echo get_class( $object );
}
// Just a Demonstration Method
public function print_parent_property_a()
{
// Reach into the referred parent object, and get it’s property
print $this->parent_object->property_a;
}
}
?>
<?php
$my_object = new A();
echo “<br />”;
$my_object->property_a = ‘–Rz Rasel Inner Outer Class’;
$my_object->child_object->print_parent_property_a();
//echo get_class( $object );
//echo get_parent_class( $this );
?>

Advertisements
By Rz Rasel Posted in Php

2 comments on “PHP Child Class Accessing Parent Variable Problem

  1. Hey. Sorry to trouble you but I happened to run across your blog website and noticed you are using the exact same theme as me. The only issue is on my blog, I’m battling to get the design and style looking like yours. Would you mind contacting me at: Kort6036@gmail.com so I can get this figured out. By the way I’ve bookmarked your web site: https://arzrasel.wordpress.com/2012/07/24/php-child-class-accessing-parent-variable-problem/ and will certainly be visiting often. Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s