PHP挂起在循环上,从不发出请求
我有一个RESTful API,需要使用CURL与之交互。我创建了一个包装器类,它有一个带有CURL代码的静态函数PHP挂起在循环上,从不发出请求,php,rest,oop,Php,Rest,Oop,我有一个RESTful API,需要使用CURL与之交互。我创建了一个包装器类,它有一个带有CURL代码的静态函数 class ApiInvoke { public static function execute($username, $password, $endpoint, $data = array(), $options = array()) { //Rest of the CURL code goes here..... } } 我创建了一个
class ApiInvoke
{
public static function execute($username, $password, $endpoint, $data = array(), $options = array())
{
//Rest of the CURL code goes here.....
}
}
我创建了一个类,在其中调用静态APIVokve类方法来实际执行API调用。下面是上面ApiInvoke类的使用者类
需要api_invoke.php
class FlowgearConnect
{
//Properties go gere
public function getResults($model, $workflow, $data)
{
$endpoint = $this->getEndpoint($model, $workflow);
$results = array();
if(!is_null($endpoint)){
$results = ApiInvoke::execute('username', 'password', $endpoint, $data array('timeout' => 30));
}
return $results;
}
//....
}
然后我有一个ParentClass类,它创建了FlowgearConnect对象的一个实例,该实例可用于子类。但是,所有子类都在同一父类内处理
class ParentClass
{
private $Flowgear;
public function init()
{
$this->Flowgear = new FlowGearConnect(); //Assuming it has been required somewhere
}
}
然后我们可能有ChildClassA和ChildClassB,它们扩展了ParentClass。通过扩展父类的子类变量,它们已经可以访问$this->Flowgear对象的实例,因为FlowgearConnect类的使用方式如下:
class ChildClassA
{
public function getResults()
{
$results = $this->Flowgear->getResults('child_a', 'latestEvents', array());
}
}
ChildClassB具有非常相同的功能,或者相当精确,只是它可能负责获取订单列表
这些子类在父类中的处理方式如下所示:
//A function inside the ParentClass to process ChildClassA and ChildClassB
public function processModules()
{
$modules = $request->getModules();
foreach($modules as $module){
require_once "modules/' . $module;
$Module = new $module();
$Module ->getResults();
}
}
沿着这条路线的某些东西是不对的。。。。基本上,扩展类创建子类使用的类的实例。不知何故,这里有些地方不对劲,我想这一切都与我没有使用辛格尔顿有关。如果我知道如何使用CURL,我就可以了。我真傻,因为Rayhan的Http客户机类,我从来没有想过我永远无法创建CURL对象的一个实例 基本上我想要的是创建一个CURL单例类,这样我就不会一次又一次地创建同一对象的实例 下面是我如何实现这一目标的基本情况:
class Flowgear
{
static private $_instance;
//Rest properties here...
public function __cosntsruct()
{ $this->_token = $this->_username .':'. $this->_passoword; }
public function execute()
{
//Call a class that handles the actual API invocation passing all relevant data
}
static public function &getInstance()
{
if(self::$_instance == null){
self::$_instance = new self;
}
return self::$_instance;
}
}
然后,通过调用Flowgear::getInstance,我只得到该类的一个实例 我想我们需要更多的代码。我需要看看这个类的内部工作。我刚刚看到了这个链接,它实际上就是我想要实现的。我将研究代码,看看是否可以做类似的事情,因为最终我希望能够拥有Flowgear对象的一个实例,并随意创建多个对象。两点。在php中,默认情况下对象是通过引用传递的,因此不需要在getInstance中使用&。而且,这整件事被认为是一种反模式,因为它只不过是一个被美化的全球模式。谢谢你的第一点。我选择使用Singleton是因为我使用的是遗留代码,我希望我的更改与代码中使用的数据库Singleton完全相同。感谢@TwiStar的反馈