Skip to main content
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
added 1821 characters in body
Source Link
Justme
  • 1.3k
  • 1
  • 10
  • 30

Addition

Thxs, with the help of the answers it is working now. I saw the excellent video, change the statements, disable my own autoloading procedure.

As a result of this I should add in my classes the use statement for every object I use. The only thing I find a little bit strange is the following.

In every php file with a class I should add the use statement for every object.

Example 1.

// Define namespaces and objects
namespace Drupal\tb\library;
use SplFileInfo;
class log{
}

Example 2

namespace Drupal\tb;

use Drupal\tb\library\log;
use Drupal\tb\datacollector\datacollector;
use Drupal\tb\datacalculator\datacalculator;
use Drupal\tb\scenario\scenario;

class traderbot{
    

    public function start(){

        (new log)->add(0,"Start data collector");
        $collector = new datacollector;
        ...

        (new log)->add(0,"Start data calculator");
        $indicators = new datacalculator;
        ...

        (new log)->add(0,"Start scenario handler");
        $sc_handler = new scenario;
        ...

    }

}

I red some other documentation and QA and understand dat wild card loading of namespacing is not possible. https://stackoverflow.com/questions/29851099/how-to-load-all-classes-of-a-namespace but I also red somewhere that as long as the class is in the same namespace it is not needed to put the use statement for this object. So then I come to the following questions for verification:

Q1) Adding a standard use statement for SPL looks very strange for me. Is this correct?

Q2) Adding a long list of several use statements from unique objects in the same namespace is very strange for me as well. Is this correct?

Addition

Thxs, with the help of the answers it is working now. I saw the excellent video, change the statements, disable my own autoloading procedure.

As a result of this I should add in my classes the use statement for every object I use. The only thing I find a little bit strange is the following.

In every php file with a class I should add the use statement for every object.

Example 1.

// Define namespaces and objects
namespace Drupal\tb\library;
use SplFileInfo;
class log{
}

Example 2

namespace Drupal\tb;

use Drupal\tb\library\log;
use Drupal\tb\datacollector\datacollector;
use Drupal\tb\datacalculator\datacalculator;
use Drupal\tb\scenario\scenario;

class traderbot{
    

    public function start(){

        (new log)->add(0,"Start data collector");
        $collector = new datacollector;
        ...

        (new log)->add(0,"Start data calculator");
        $indicators = new datacalculator;
        ...

        (new log)->add(0,"Start scenario handler");
        $sc_handler = new scenario;
        ...

    }

}

I red some other documentation and QA and understand dat wild card loading of namespacing is not possible. https://stackoverflow.com/questions/29851099/how-to-load-all-classes-of-a-namespace but I also red somewhere that as long as the class is in the same namespace it is not needed to put the use statement for this object. So then I come to the following questions for verification:

Q1) Adding a standard use statement for SPL looks very strange for me. Is this correct?

Q2) Adding a long list of several use statements from unique objects in the same namespace is very strange for me as well. Is this correct?

Source Link
Justme
  • 1.3k
  • 1
  • 10
  • 30

Why do I have conflicts in namespacing?

I'm building a traderbot for drupal and I have problems with the namespacing. Something is not working or I do not understand it correctly. I looked on several pages and questions but could not understand it.

The start

I runned all code without namespaces. Then it is working well !!!

I included a project called ccxt. In this project there are classes with the same name. They have a class called kraken and I have as well. There classes are in namespace ccxt.

So mine class should be namespaced. Which is very obvious.

As I do call from drush I need to have a own autoloader.

function autoloader_traderbot( $class_name ){

  // Because of namespaces $class_name could be string/string therefore only the last string should be taken
  if (($pos = strpos($class_name, '\\')) !== FALSE) { 
    $class_name = substr($class_name, $pos+1); 
    (new log)->add(4,'Class name adjusted. Namespace removed from class path.');

  }

  (new log)->add(4,'Searching for loading class  name  : ' . $class_name );

  $dir_iterator = new RecursiveDirectoryIterator(CLASS_DIR);
  $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

  foreach ($iterator as $file) {
    if($file->getFilename() == $class_name . ".class.php"){
      (new log)->add(4,'Loading class file name (.class.php) : ' . $class_name . ' in filename ' . $file );
      require_once tb_adjustfilename($file);
    }

    if($file->getFilename() == $class_name . ".php"){
      (new log)->add(4,'Loading class file name (.php): ' . $class_name . ' in filename ' . $file);
      require_once tb_adjustfilename($file);      
    };
  }

} 

So I add in kraken.class.php

namespace traderbot;

Then calling this object is working fine.

But then the problems are there

I also get

Error: Class 'traderbot\log' not found in traderbot\kraken->update_ohlc() (line 48 of C:\www\tb\modules\custom\tb\src\exchanges\kraken.class.php) #0 [31;40m[1m[error][0m

I checked log.class.php.

This is not namespaced so I add

namespace traderbot;

But then I get from php

Error: Class 'traderbot\SplFileInfo' not found in traderbot\log->add() (line 66 of C:\www\tb\modules\custom\tb\src\library\log.class.php).
Drush command terminated abnormally due to an unrecoverable error.    

But now I'm lost. Because this is just a normal php object so why is it going wrong?