PHP:从另一个扩展类文件调用另一个公共类方法

时间:2018-07-10 10:05:58

标签: php

我如何从另一个扩展类文件中调用公共类函数,因为这个问题是api.php。我希望能够从api.php类api扩展休息的track.php中调用public function track_osc_delivery(){ }。请在下面的代码中进行修复,这很艰巨,我尝试将api.php包含在track.php中,但这对解决它没有帮助。

// rest.php

  class Rest {
    protected $request;
    protected $serviceName;
    protected $param;

    public function __construct(){
        if($_SERVER['REQUEST_METHOD'] !== 'POST'){
            $this->throwError(REQUEST_METHOD_NOT_VALID, 'Request Method is not valid');
        }
        $handler = fopen('php://input', 'r');
        $this->request = stream_get_contents($handler); 
        $this->validateRequest();       
    }

        public function validateParameter($fieldName, $value, $dataType, $required = true){

        }

          public function processApi(){
        $api = new API;
        $rMethod = new reflectionMethod('API', $this->serviceName);
        if(!method_exists($api, $this->serviceName)){
           $this->throwError(API_DOST_NOT_EXIST, "API does not exist");
        }
        $rMethod->invoke($api);
    }
    }

// api.php

 class Api extends Rest {
public $dbConn;  

 public function __construct(){
    parent::__construct();  
    $db = new DbConnect;
    $this->dbConn = $db->connect();

}


 public function track_osc_delivery(){

$waybill_number = $this->validateParameter('track_waybill_number', $this->param['track_waybill_number'], STRING, true);

$password_for_shipments = $this->validateParameter('track_password_sent', $this->param['track_password_sent'], STRING, true);

}
 }

// track.php

  require_once('functions.php');
   $api = new Api;
   $api->processApi();

   //WANT TO CALL public function track_osc_delivery() here

// functions.php

       spl_autoload_register(function($className){
         $path = strtolower($className) . ".php";
 if(file_exists($path)){
             require_once($path);
         } else {
             echo "File $path is not found.";
             exit();
         }

2 个答案:

答案 0 :(得分:1)

尝试一下,它应该可以在track.php中工作,因为Loek表示它是继承的,并且对于进程api也可以包含在脚本中

更改密码

 require_once('functions.php');
 $api = new Api;
 $api->processApi();

  require_once('functions.php');
  $api = new Api;
  $api->track_osc_delivery();

快乐的编码...

答案 1 :(得分:1)

据我所知,我认为这将通过范围解析运算符以及直接使用$ api-> track_osc_delivery();
我已删除某些功能的内部代码以防止错误。

(1)rest.php

<?php
class Rest {
    protected $request;
    protected $serviceName;
    protected $param;

    public function __construct(){
    }

    public function validateParameter($fieldName, $value, $dataType, $required = true)
    { }

    public function processApi(){
        $api = new API;
        return $api;
    }
}

?>

(2)api.php

<?php
require_once('rest.php');
class Api extends Rest {
    public $dbConn;  

    public function __construct(){
        parent::__construct();  
    }


    public function track_osc_delivery(){
        return "function called";

    }
}

?>

(3)track.php

<?php
require_once('api.php');
$api = new Api;
echo "<pre>";
print_r($api);
echo "</pre>";
$api->processApi();

//echo $api->track_osc_delivery(); //this will also worked.
$test = $api::track_osc_delivery();
echo $test;
?>

可以随时询问。