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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?