draxu26480 2018-04-19 10:44
浏览 82
已采纳

使用名称空间与现有代码的项目

I am trying to integrate PHPML with an existing project. PHPML uses namespaces and I have little experience using them.

I am able to run PHPML directly through my controller however when integrating it in to a class method I get the following error

Parse error: syntax error, unexpected 'use' (T_USE) in... on line 4

My class method:

class learn{
  public function return_adjustments(){
    include 'application/vendor/autoload.php';
    use Phpml\Regression\LeastSquares;
    use Phpml\Exception\FileException;

    $samples = $this->csv_to_array('samples.csv');
    $targets = $this->csv_to_array('targets.csv');

    $regression = new LeastSquares();
    $regression->train($samples, $targets);
}

Is it possible to do this correctly? I'm struggling on the concept of namespaces.

  • 写回答

1条回答 默认 最新

  • doushangxianq07480 2018-04-19 10:52
    关注

    Put the "use" statements at the very top of your file in order to import them properly.

    include 'application/vendor/autoload.php';
    
    class learn{
      public function return_adjustments() {
    
        $samples = $this->csv_to_array('samples.csv');
        $targets = $this->csv_to_array('targets.csv');
    
        $regression = new Phpml\Regression\LeastSquares();
        $regression->train($samples, $targets);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?