0

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.

user1949366
  • 465
  • 2
  • 6
  • 17
  • You are breaking the rules of the `importing` in PHP, checkout the right scope of [`use` keyword](http://php.net/manual/en/language.namespaces.importing.php) – hassan Apr 19 '18 at 10:48
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – aynber Apr 19 '18 at 14:55

1 Answers1

1

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);
}
tionsys
  • 181
  • 8
  • I tried this, however, it throws Fatal error: Uncaught Error: Class 'LeastSquares' not found in – user1949366 Apr 19 '18 at 11:01
  • What happens if you try `$regression = new Phpml\Regression\LeastSquares();`? Depending on if this throws an exception too, chances are the autoload mechanism doesn't work properly – tionsys Apr 19 '18 at 11:05
  • Of course! Works perfectly. If you'd like to change your answer ill accept it! Thank you! – user1949366 Apr 19 '18 at 11:12