我正在按照Tutorials Point上的步骤尝试实现网桥设计模式。我正在将代码从Java转换为PHP,并更改了一些名称。
问题是,当我尝试将具体的桥实现程序类传递给具体的类实现接口时,会引发错误。
在代码中更容易理解:
// LaunchApi.php
interface LaunchApi
{
public function launch();
}
// RedEngine.php
class RedEngine implements LaunchApi
{
public function launch()
{
echo "The red engine is really fast!!!";
}
}
// Rocket.php
abstract class Rocket
{
protected $launchApi;
protected function __construct($launchApiImplementer)
{
$this->launchApi = $launchApiImplementer;
}
public abstract function launch();
}
// FullRocket.php
class FullRocket extends Rocket
{
public function __construct($launchApi)
{
parent::__construct($launchApi);
}
public function launch()
{
$this->launchApi->launch();
}
}
// LaunchingScript.php
$redEngine = new RedEngine();
$redEngine->launch(); // this works
$redRocket = new FullRocket($redEngine);
$redRocket.launch(); // this won't work
错误抛出是:
design-patterns\Bridge>php LaunchingBridge.php
The red engine is really fast!!!
Fatal error: Call to undefined function launch() in \design-patterns\Bridge\LaunchingBridge.php on line 24
我尝试使用&通过引用传递,但这只会更改错误。
答案 0 :(得分:1)
是,应该是$redRocket->launch();
,而不是$redRocket.launch();
就像奈杰尔·仁所说的