Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Magic Methods not finding class name
  • I am trying to port an API into Fuel and am currently going through the code and making sure the Class names and directory structure are correct. I have however come across a problem which I could appreciate some guidance on. The API uses an magic method to determine which class to load based on the name of the method supplied. However, if I let it work how it wants to I get a class not found message. If I explicitly give it the class name and pass in the parameters dynamically as the magic method receives them, the class is found absolutely fine. Code below, doesn't work, does work, error message.
    /**
         * Main magic method
         * @param string $name
         * @param array $arguments
         * @return mixed
         */
        public function __call($name, $arguments)
        {
            $className = 'Commands_' . ucfirst($name);
            //if (class_exists($className)) {
      //try {
                $command = new Commands_Connect($this);//$className($this);
                $this->canExecute($command);
                call_user_func_array(array(
                    $command,
                    'setAttribs'), $arguments);
    
                $command->prepare();
                $data = $command->execute();
    
                if ($command->opType == Commands_Abstract::CONNECT) {
                    $this->connected = true;
                }
                if ($command->opType == Commands_Abstract::DB_OPEN) {
                    $this->DBOpen = true;
                }
                if ($command->opType == Commands_Abstract::DB_CLOSE) {
                    $this->DBOpen = false;
                    $this->active = false;
                    $this->socket = null;
                }
                return $data;
           // } catch (Exception $e) {
      // } else {
           //     throw new OrientDBWrongCommandException('Command ' . $className . ' currenty not implemented');
           // }
    
        }
    
    /**
         * Main magic method
         * @param string $name
         * @param array $arguments
         * @return mixed
         */
        public function __call($name, $arguments)
        {
            $className = 'Commands_' . ucfirst($name);
            //if (class_exists($className)) {
      //try {
                $command = new Commands_Connect($this);//$className($this);
                $this->canExecute($command);
                call_user_func_array(array(
                    $command,
                    'setAttribs'), $arguments);
    
                $command->prepare();
                $data = $command->execute();
    
                if ($command->opType == Commands_Abstract::CONNECT) {
                    $this->connected = true;
                }
                if ($command->opType == Commands_Abstract::DB_OPEN) {
                    $this->DBOpen = true;
                }
                if ($command->opType == Commands_Abstract::DB_CLOSE) {
                    $this->DBOpen = false;
                    $this->active = false;
                    $this->socket = null;
                }
                return $data;
           // } catch (Exception $e) {
      // } else {
           //     throw new OrientDBWrongCommandException('Command ' . $className . ' currenty not implemented');
           // }
    
        }
    
    ErrorException [ Error ]: Class 'Commands_Connect' not found
    
    PKGPATH/orientdb/classes/orientdb.php @ line 294
    
    289    public function __call($name, $arguments)
    290    {
    291        $className = 'Commands_' . ucfirst($name);
    292        //if (class_exists($className)) {
    293        //try {
    294            $command = new $className($this);
    295            $this->canExecute($command);
    296            call_user_func_array(array(
    297                $command,
    298                'setAttribs'), $arguments);
    
  • Might be a namespace issue. Class names in strings are always interpreted from global, so you might have to prefix them by "\\packagename\\".
  • Thanks. Worked a treat. I thought I had tried that but obviously not!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion