Jump to content
Larry Ullman's Book Forums
bartleyg

Console Application - Php Fatal Error: Cannot Redeclare Class

Recommended Posts

Larry et al.,

After working through several web applications I have started to work on a console application.  To get started I created a simple class in in the commands folder called SayHelloConsole.php.  When I try to run the application ./yiic SayHello I receive the following error:

 

$ ./yiic sayhello
PHP Fatal error:  Cannot redeclare class SayHello in /var/www/myapp/protected/commands/SayHelloCommand.php on line 6

 

I have no other classes with the same name and I cannot track down where an additional import is taking place.  I have tried removing:

'import'=>array(
        'application.commands.*',
    ),

 

from console.php, but then yiic fails because it cannot find the class file.  I have googled and reviewed other reports of the problem, however I cannot find a solution to my problem.  I have also tried the same command on two different Yii installations and still have the same issue.  If I don't import 'application.commands' yiic cannot find the file and if I do, I get the 'cannot redeclare class' error.  Any suggestions?

Thanks,

Bart

 

My application is in the file SayHelloCommand.php

<?php
class SayHello extends CConsoleCommand{
    public function run($args){
        echo "Hello!\n";
    }
}

 

 

My console configuration is console.php

<?php
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'Console',
    // preloading 'log' component
    
    // autoloading model and component classes
    'import'=>array(
        'application.commands.*',
    ),

);
 

 

 

Share this post


Link to post
Share on other sites

Is there any more to the error message? Other than that, I hate to say that the problem is almost certainly that you do, in fact, have a SayHello class defined somewhere else in your code. You could change your class name to something really random to confirm.

Share this post


Link to post
Share on other sites

Larry,

I created a new file FurryWhiteBunnyCommand.php with the following class:

<?php

class FurryWhiteBunny extends CConsoleCommand{
    public function bunnies(){
       echo "I am a bunny\n";
    }
}

 

and I get the following:

$ ./yiic furrywhitebunny bunnies

PHP Fatal error:  Cannot redeclare class FurryWhiteBunny in /opt/local/www/myapp/protected/commands/FurryWhiteBunnyCommand.php on line 7

 

That is the only error message.

 

No other bunny files/classes on my system.  If I type ./yiic help it show the command:

$ ./yiic help
Yii command runner (based on Yii v1.1.14)
Usage: ./yiic <command-name> [parameters...]

The following commands are available:
 - furrywhitebunny
 - getxml
 - message
 - migrate
 - sayhello
 - shell
 - webapp

To see individual command help, use the following:
   ./yiic help <command-name>

 

The rest of my Yii application seems to work great.  That is why I'm stumped on this.  Is there another way to autoload other than

'import'=>array(
        'application.commands.*',
    ),

in the console.php file?

Thanks,

Bart

Share this post


Link to post
Share on other sites

Larry,

Just to make sure I didn't have some other custom configuration issue causing the problem I created a new web application on another machine.  I then created the commands/SayHelloCommand.php file identical to the one I used earlier and attempted to run the console command:

$ ./yiic sayhello
PHP Error[2]: include(SayHelloCommand.php): failed to open stream: No such file or directory
    in file /opt/local/www/yii/framework/YiiBase.php at line 427
#0 /opt/local/www/yii/framework/YiiBase.php(427): autoload()
#1 unknown(0): autoload()
#2 /opt/local/www/yii/framework/console/CConsoleCommandRunner.php(170): spl_autoload_call()
#3 /opt/local/www/yii/framework/console/CConsoleCommandRunner.php(67): CConsoleCommandRunner->createCommand()
#4 /opt/local/www/yii/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()
#5 /opt/local/www/yii/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#6 /opt/local/www/yii/framework/yiic.php(33): CConsoleApplication->run()
#7 /opt/local/www/html/myapp/protected/yiic.php(7): require_once()
#8 /opt/local/www/html/myapp/protected/yiic(4): require_once()

I then changed config/console.php to add the import statement so  autoloading could find the new command:
<?php

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
        'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
        'name'=>'My Console Application',

        // preloading 'log' component
        'preload'=>array('log'),
        'import'=>array(
                'application.commands.*',
        ),
);

 

I then re-ran the command and received the same results.

$ ./yiic sayhello
PHP Fatal error:  Cannot redeclare class SayHello in /opt/local/www/html/myapp/protected/commands/SayHelloCommand.php on line 6

 

I'm using Yii 1.14, but I also tried it with 1.13 and 1.12.

Thanks,

Bart

Share this post


Link to post
Share on other sites

Very, very strange. Sounds like you've done your due diligence here. May be a bug in Yii, although not one I've seen before.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...