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

Object of class Person could not be converted to string

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

#!/usr/bin/php
<?php class Person{
public $name = 'ghostwu';
public $age = 20; function __toString(){
return json_encode( $this );
}
} echo new Person();
?>

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

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可以使用匿名类简化

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. flask_maple使用文档

    安装 To install Flask-Maple: pip install flask-maple Or alternatively, you can download the repository ...

  2. 浅谈getResource方法

    项目经常会读取一些配置文件, 因此getResource方法便能够起到重要作用 使用时主要是两种方法, 一个是字节码文件Class类, 另一个是ClassLoader类加载器 使用Class类时有两种 ...

  3. 【翻译】 Windows 内核漏洞学习—空指针解引用

    Windows Kernel Exploitation – NullPointer Dereference 原文地址:https://osandamalith.com/2017/06/22/windo ...

  4. Unable to preventDefault inside passive event listener due to target being treated as passive?

    使用滚动时候,新版google浏览器,会弹出如下的警告. 解决方法,可以加上* { touch-action: none; } 这句样式去掉. 其原因:https://developers.googl ...

  5. Shell - 简明Shell入门03 - 字符串(String)

    示例脚本及注释 #!/bin/bash str="Shell" str2="Hello $str !" str3="Hello ${str} !&qu ...

  6. widows本地-xshell实现远程连接linux服务器图形界面

    本地环境远程连接linux图形界面,常用的实现工具有,VNC.Puty.Xshell等,这里我们用的xshell manager: Xmanager简介:Xmanager是一个运行于 Windows平 ...

  7. Django Admin实现三级联动(省市区)

    通过自定义Admin的模板文件实现省市区的三级联动.要求创建记录时,根据省>市>区的顺序选择依次显示对应数据. 修改记录时默认显示已存在的数据. Model class Member(mo ...

  8. Sublime text3 Package Control不能使用

    Package Control打开时提示"There are no availabel for installation"的两个处理办法: 第一种: ping一下sublime的服 ...

  9. COM+时代的自动事务

    最近看公司的遗留项目代码,调试的时候发现经常报分布式事务错误,可是整个代码里没有看见开启过事务,于是开始研究,发现了这个.Net Framework1.1时代的产物. namespace Busine ...

  10. rabbitmq 部署(二进制和rpm)与常用命令

    目录 一 rabbitmq 简介 二 erlang 安装 三 rabbitmq rpm安装 (二进制安装和rpm安装二选一) 四 rabbitmq 二进制安装(rpm 安装和二进制安装二选一) 五 初 ...