当echo一个对象的时候,会报错误

Object of class Person could not be converted to string

我们可以通过魔术方法__tostring()  把对象转成字符串

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. class Person{
  5. public $name = 'ghostwu';
  6. public $age = 20;
  7.  
  8. function __toString(){
  9. return json_encode( $this );
  10. }
  11. }
  12.  
  13. echo new Person();
  14. ?>

继续改造php静态变量与方法与phar的使用

ghostconfig.php

  1. <?php
  2. class ghostconfig{
  3. public $projName = '';
  4. public $author = '';
  5.  
  6. function __tostring(){
  7. return json_encode( $this );
  8. }
  9. }
  10.  
  11. ?>

ghostinit.php

  1. require( "ghostconfig.php" );
  2. class ghostinit{
  3. static $v = 'ghost version is 1.1';
  4.  
  5. static function init(){
  6. $config = new ghostconfig();
  7. echo "pls input project name?" . PHP_EOL;
  8. $config->projName = fgets( STDIN );
  9.  
  10. echo "pls input author?" . PHP_EOL;
  11. $config->author = fgets( STDIN );
  12.  
  13. echo "您输入的项目信息如下:" . PHP_EOL;
  14. echo $config;
  15. }
  16.  
  17. static function getConfig( $conf ){
  18. $std = new stdClass();
  19. foreach( $conf as $k => $v ){
  20. $std->$k = $v;
  21. }
  22. return $std;
  23. }
  24.  
  25. function __tostring(){
  26. return json_encode( $this );
  27. }
  28.  
  29. static function make(){
  30. $pchar=new Phar("ghost.phar");
  31. $pchar->buildFromDirectory(dirname(__FILE__));
  32. $pchar->setStub($pchar->createDefaultStub('ghost'));
  33. $pchar->compressFiles(Phar::GZ);
  34. }
  35.  
  36. static function __callstatic( $m, $args ){
  37. echo 'error function';
  38. }
  39.  
  40. }

php7可以使用匿名类简化

php魔术方法__tostring的应用的更多相关文章

  1. PHP魔术方法__tostring()篇

    下面是关于__tostring 的运用 header('Content-type:text/html;charset="utf-8"'); /*存在内置方法_tostring()的 ...

  2. php类知识---魔术方法__toString,__call,__debugInfo

    <?php class mycoach{ public function __construct($name,$age) { $this->name = $name; $this-> ...

  3. PHP面向对象魔术方法之__toString函数

    l 基本介绍: 当我们希望将一个对象当做字符串来输出时,就会触发__toString魔术方法. <?php header('content-type:text/html;charset=utf- ...

  4. php魔术方法使用场景

    php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...

  5. PHP面向对象常见的关键字和魔术方法

    在PHP5的面向对象程序设计中提供了一些常见的关键字,用来修饰类.成员属性或成员方法,使他们具有特定的功能,例如final.static.const等关键字.还有一些比较实用的魔术方法,用来提高类或对 ...

  6. PHP魔术方法小结.md

    说明 魔术方法就是在特定场景下不需要调用而自动执行的方法.因为有魔术方法,所以我们的类可以写得很灵活~ __construct #构造方法,在类被实例化时自动调用,一般用于初始化操作; __destr ...

  7. PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload

    1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...

  8. PHP 魔术方法 __clone __toString(五)

    __clone() - 当对象克隆的时候自动加载此方法 __toString() - 当对象需要echo打印输出的时候自动加载此方法 __clone() <?php class example{ ...

  9. PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload

    1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...

随机推荐

  1. Poetry

    1. Absence to love is what wind is to fire. It extinguishes the small; It inflames the great. 2. It ...

  2. Spring学习笔记1——IOC: 尽量使用注解以及java代码

    在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...

  3. Git-遇到的问题以及解决方法

    1.将本地内容推送到远程仓库后,远程仓库里的文件夹不可点击 原因:在本地添加文件夹A时,又在A里使用了git init命令 解决:删除文件夹A,再重新添加过 2.其他人推送不了内容到远程仓库 原因:权 ...

  4. iPhone X Web 设计

    原文地址:https://webkit.org/blog/7929/designing-websites-for-iphone-x/ 开箱即用(开发者无需进行任何设置),在iPhone X中,Safa ...

  5. vue教程2-03 vue计算属性的使用 computed

    vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...

  6. Why does Http header contains "X-SourceFiles"?

    Question: Using a FileStreamResult in ASP.NET MVC 3, I get a response header like X-SourceFiles =?UT ...

  7. android studio 一直卡在Gradle:Build Running的解决办法

    转:android studio 一直卡在Gradle:Build Running的解决办法   在使用AS开发安卓应用程序的时候经常会遇到Gradle build running一直在运行甚至卡死的 ...

  8. 【转载】win7搜索文件怎么搜索文件名中带圆括号的文件

    System.FileName:~=“(” 这样就行. 括号上加个引号 ~= 是包含的意思, ~< 是以什么为开头, = 是以什么为名, ~! 是不包含 来自: http://www.lao8. ...

  9. ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: Cannot run program "hive": error=2, No such file or directory

    原因是hive没有设置环境变量 1,vim /etc/profile  (切换root用户) 2.source /etc/profile

  10. CentOS 6.7 下 PostgreSQL 9.5 的安装与配置

    #yum方式安装(不同的系统版本对应的版本也不同) yum install postgresql-server #安装指定版本 yum install https://download.postgre ...