I have searched hi and low for a solution to this without any luck whatsoever, so i posting as a last resort in hope someone can help me.
class Main
{
private $data = array();
private $a;
private $b;
function __contruct()
{
}
public function a()
{
if (!is_object($this->a))
{
$this->a = new A();
}
return $this->a;
}
public function b()
{
if (!is_object($this->b))
{
$this->b = new B();
}
return $this->b;
}
protected function set_data($value)
{
$this->data = $value;
}
}
class A extends Main
{
function __contrust()
{
parent::_construct();
}
public function test($value)
{
$this->set_data($value);
return $this;
}
}
class B extends Main
{
function __contrust()
{
parent::_construct();
}
public function test($value)
{
$this->set_data($value);
return $this;
}
public function get_data()
{
print_r($this->a);
}
}
$m = new Main();
$m->a()->test('add A class data');
$m->b()->test('add B class data');
$m->b()->get_data(); // want to use class A object data here
edit
[a:private] => A Object
(
[data:private] => Array
(
[0] => Array
(
[name] => foo
[data] => 65
)
[1] => Array
(
[name] => bar
[data] => 65
)
)
[a:private] =>
[b:private] =>
)
I have a method in the main class that finds the name key im looking for and returns its index so i can manipulate its data which works fine from its calling class. However class B needs to be able to set its own data back to the data array as well as get a hold of class A object data so i can manipulate its data.
So as you can see after i instantiate the main class and fire A class i want to be able to use the object data of A in my B class...
Would greatly appreciate any help possible.
Thanks,
Darren
That's the code you may want:
class Main
{
private $data = array();
protected $a;
protected $b;
function __construct()
{
}
public function a()
{
if (!is_object($this->a))
{
$this->a = new A();
}
return $this->a;
}
public function b()
{
if (!is_object($this->b))
{
$this->b = new B();
}
return $this->b;
}
protected function set_data($value,$object)
{
$object->data = $value;
}
}
class A extends Main
{
function __contruct()
{
parent::_construct();
}
public function test($value,$object)
{
$this->set_data($value,$object);
return $this;
}
}
class B extends Main
{
function __contruct()
{
parent::_construct();
}
public function test($value,$object)
{
$this->set_data($value,$object);
return $this;
}
public function get_data()
{
print_r($this);
}
}
$m = new Main();
$m->a()->test('add A class data',$m->b());
$m->b()->test('add B class data',$m->a());
$m->b()->get_data(); // want to use class A object data here
and thats the output:
B Object
(
[data:private] => add A class data
[a:protected] =>
[b:protected] =>
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.