【发布时间】:2009-08-06 17:30:46
【问题描述】:
在 PHP 中给出以下内容:
<?php
class foo {
public $bar;
function __construct() {
"Foo Exists!";
}
function magic_bullet($id) {
switch($id) {
case 1:
echo "There is no spoon! ";
case 2:
echo "Or is there... ";
break;
}
}
}
class bar {
function __construct() {
echo "Bar exists";
}
function target($id) {
echo "I want a magic bullet for this ID!";
}
}
$test = new foo();
$test->bar = new bar();
$test->bar->target(42);
我想知道'bar' 类是否可以调用'foo' 类的'magic bullet' 方法。 'bar' 实例包含在 'foo' 实例中,但与它没有父/子关系。实际上,我有许多不同的“bar”类,“foo”在一个数组中,每个都做了一些与 $id 不同的事情,然后想要将它传递给“magic_bullet”函数以获得最终结果,所以禁止一个结构类关系的变化,是否可以访问“容器”实例的方法?
【问题讨论】:
标签: php