В этом разделе используются примеры перед модификацией класса и статической переменной: php генерирует конфигурационные файлы на основе параметров командной строки
ghostinit.php:
buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
}
?>привидение:
#!/usr/bin/php
= 2 ) {
$argv[1] == '-v' && $result = ghostinit::$version;
$argv[1] == 'make' && ghostinit::make();
$argv[1] == 'init' && ghostinit::init();
}
echo $result . PHP_EOL;Результат выполнения:
ghostwu@dev:~/php/php1/3$ ls done ghost ghostinit.php ghostwu@dev:~/php/php1/3$ ./ghost init pls input project name? test pls input author? ghostwu //The project information you entered is as follows: test ghostwu ghostwu@dev:~/php/php1/3$ ls done ghost ghostinit.php ghostwu@dev:~/php/php1/3$ ./ghost make ghostwu@dev:~/php/php1/3$ ls done ghost ghostinit.php ghost.phar ghostwu@dev:~/php/php1/3$ ./ghost -v ghost version is 1.1 ghostwu@dev:~/php/php1/3$
callstatic продолжает трансформироваться:
ghostinit.php:
buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
static function __callstatic( $m, $args ){
echo 'error function';
}
}
?>привидение:
#!/usr/bin/php
= 2 ) {
$p = $argv[1];
if( substr( $p, 0, 1 ) == '-' ) {
$p = substr( $p, 1 );
$result = isset( ghostinit::$$p ) ? ghostinit::$$p : 'error';
}else {
$result = ghostinit::$p();
}
}
echo $result . PHP_EOL;Разделите конфигурацию на класс
ghostconfig.php: аннотируйте эти два свойства, и они также могут работать нормально. PHP позволяет динамическое добавление переменных-членов (свойств класса)
ghostinit.php
projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
$conf->author = fgets( STDIN );
echo "The project information you entered is as follows:" . PHP_EOL;
echo json_encode( $conf );
}
static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
static function __callstatic( $m, $args ){
echo 'error function';
}
}
?>Использование класса верхнего уровня stdClass вместо config class сокращает один класс. В настоящее время этот конфигурационный класс используется только один раз, что можно снова упростить с помощью stdClass
projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
$conf->author = fgets( STDIN );
echo "The project information you entered is as follows:" . PHP_EOL;
echo json_encode( $conf );
}
static function make(){
$pchar=new Phar("ghost.phar");
$pchar->buildFromDirectory(dirname(__FILE__));
$pchar->setStub($pchar->createDefaultStub('ghost'));
$pchar->compressFiles(Phar::GZ);
}
static function __callstatic( $m, $args ){
echo 'error function';
}
}
?>Создайте информацию о конфигурации, снова упростите и превратите ее в общий модуль:
static function init(){
echo "pls input project name?" . PHP_EOL;
$projName = fgets( STDIN );
echo "pls input author?" . PHP_EOL;
$author = fgets( STDIN );
echo "The project information you entered is as follows:" . PHP_EOL;
echo json_encode( self::getConfig( [ 'proj_name' => $projName, 'author' => $author ] ) );
}
static function getConfig( $conf ){
$std = new stdClass();
foreach( $conf as $k => $v ){
$std->$k = $v;
}
return $std;
}Оригинал: “https://programmer.help/blogs/php-static-variables-and-methods-and-the-use-of-phar.html”