Skip to main content
1 of 3
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?

Justme
  • 1.3k
  • 1
  • 10
  • 30