2013-05-13 55 views
0

由于控制台命令只允许声明config()和​​函数,我如何声明用户定义的函数并调用它们?Symfony2 - 从命令访问用户定义的功能

+0

请特别说明你想要什么?当从控制台执行时,只调用'execute()'。 – 2013-05-13 14:43:45

+5

你可以声明你喜欢的任何方法,并从执行中调用它们 – meouw 2013-05-13 14:45:18

回答

2

您可以定义和调用任何功能在您的Command类:

<?php 

namespace ...\Command; 

use ... 

class TestCommand extends Command 
{ 
    protected function execute(InputInterface $input, OutputInterface $output) 
    { 
     // ... 

     $this->mySuperFunction(); 
    } 

    protected function mySuperFunction() 
    { 
     // your code goes here... 
    } 
} 

如果你想输出的东西,然后通过你的输出对象给你的函数

$this->mySuperFunction($output); 

,并使用它:

protected function mySuperFunction(OutputInterface $output) 
{ 
    $output->write('hello world!'); 
}