douhui5953 2014-09-30 00:04
浏览 56
已采纳

PHP:在类中加载一个类

I have a class which is meant to "load" an another class, however I haven't been able to get it to work.

Error Message

Fatal error: Call to undefined method stdClass::echoString() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\classes\example.php on line 5

Code

My code is broken up into three main sections:

  • api.php - the class to load the other classes.
  • API/exampleExternalAPI.php - (multiple files) the classes that api.php loads

  • example.php - the file that uses the main class (api.php)

If it helps these files can be downloaded from my dropbox

api.php

<?php
/* Config */
define('pathToAPIs','API/');

/* Autoload Function */
spl_autoload_register(function($className){
    $namespace=str_replace("\\","/",__NAMESPACE__);
    $className=str_replace("\\","/",$className);
    $class=pathToAPIs.(empty($namespace)?"":$namespace."/")."{$className}.php";
    include_once($class);
});

class api {
    private $listOfAPIs;

    public $APIs;

    public function __construct($setAPI = null){
        $this->updateListOfAPIs();
        if (isset($setAPI)){
            return $this->setAPI($setAPI);
        }
    }
    public function setAPIs($setAPIs){
        $this->APIs = null; // clears a previous call to this method
        if (!is_array($setAPIs)){ // if not an array
            $setAPIs = array($setAPIs); // make array
        }
        foreach ($setAPIs as $setAPIType){
            if(in_array($setAPIType,$this->listOfAPIs)){
                $array[$setAPIType] = new $setAPIType;
            }
        }
        $this->APIs = json_decode(json_encode($array), FALSE); // convert array of required api objects to an object
        return $this->APIs;
    }
    public function getListOfAPIs($update = false){
        if ($update){
            $this->updateListOfAPIs();
        }
        return  $this->listOfAPIs;
    }
    private function updateListOfAPIs(){
        $this->listOfAPIs = null; // clears a previous call to this method
        $it = new FilesystemIterator(pathToAPIs);
        foreach ($it as $fileinfo){
            $filename = pathinfo($fileinfo->getFilename(), PATHINFO_FILENAME); // removes extension
            $this->listOfAPIs[]= $filename;
        }
    }
    public function __call($method,$args){

    }
}

API/exampleExternalAPI.php

<?php

class exampleExternalAPI {

       public function echoString($string){
           echo $string;
       }
}

example.php

<?php
require_once 'api.php';
$api = new api();
$api->setAPIs('exampleExternalAPI');
$api->APIs->exampleExternalAPI->echoString('string');

Background Info

(may give some insight to my madness)

  • I'm working on a project where I need to connect to lots of external APIs.
  • So I decided to creating a class to look after all my communications with external APIs ( not sure if best way - new to Object Oriented Programming).
  • 写回答

1条回答 默认 最新

  • dongying195959 2014-09-30 00:21
    关注

    I'm not entirely sure what problem you're trying to solve, but if your APIs is a simple stdClass instance it should work as expected:

    public function setAPIs($setAPIs)
    {
        $this->APIs = new stdClass; // clears a previous call to this method
        if (!is_array($setAPIs)) { // if not an array
            $setAPIs = array($setAPIs); // make array
        }
        foreach ($setAPIs as $setAPIType) {
            if (in_array($setAPIType, $this->listOfAPIs)) {
                $this->APIs->{$setAPIType} = new $setAPIType;
            }
        }
        return $this->APIs;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致