在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
当echo一个对象的时候,会报错误
我们可以通过魔术方法__tostring() 把对象转成字符串 #!/usr/bin/php <?php class Person{ public $name = 'ghostwu'; public $age = 20; function __toString(){ return json_encode( $this ); } } echo new Person(); ?> ghostconfig.php <?php class ghostconfig{ public $projName = ''; public $author = ''; function __tostring(){ return json_encode( $this ); } } ?> ghostinit.php require( "ghostconfig.php" ); class ghostinit{ static $v = 'ghost version is 1.1'; static function init(){ $config = new ghostconfig(); echo "pls input project name?" . PHP_EOL; $config->projName = fgets( STDIN ); echo "pls input author?" . PHP_EOL; $config->author = fgets( STDIN ); echo "您输入的项目信息如下:" . PHP_EOL; echo $config; } static function getConfig( $conf ){ $std = new stdClass(); foreach( $conf as $k => $v ){ $std->$k = $v; } return $std; } function __tostring(){ return json_encode( $this ); } 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'; } } php7可以使用匿名类简化
|
2022-08-17
2022-09-18
2022-08-17
2022-07-29
2022-08-15
请发表评论