[英]access full object data set from within another class into the main class
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.
但是,B类需要能够将其自身的数据设置回数据数组,并拥有A类对象数据,以便我可以操纵其数据。
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... 因此,正如您在实例化主类并触发A类之后所看到的,我希望能够在我的B类中使用A的对象数据...
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] =>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.