当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. CentOS修改yum源为阿里云

    yum的工具,自动去下载某个yum仓库的 rpm软件包,并且自动搜索软件下载软件依赖,如同pip3,npm等包管理工具 yum载linux中的 yum仓库是 /etc/yum.repos.d 并且在这 ...

  2. Django 实现第三方账号登录网站

    这里我们使用 django-allauth 模块来实现第三方账号验证登录,官方文档如下:https://django-allauth.readthedocs.io/en/latest/ . 安装 dj ...

  3. Tree with Small Distances(cf1029E)(树形动规)

    You are given an undirected tree consisting of \(n\) vertices. An undirected tree is a connected und ...

  4. Django(命名URL和URL反向解析)

    day67 参考: https://www.cnblogs.com/liwenzhou/articles/8271147.html#autoid-1-4-0 反向解析URL             本 ...

  5. 简单记录常用git 命令

    声明:仅作笔记用 拉取远程代码 1.git pull 2.如果需要,输入账户名密码 将本地代码推送到远程 1.git push 2.如果需要,输入账户名密码 同步远程分支 1.git fetch 2. ...

  6. Liferay-Activiti 企业特性功能介绍 (新版Liferay7)

    前言 如果你是开发者 你已经是多少次开发一个项目,一次次的用一些框架,一次次的写类似的重复的代码,一次次建表\写类和方法\写HTML\CSS\JAVASCRIPT,一次次测试,一次次的写Bug...如 ...

  7. 读取excel合并单元格内容

    def get_excel_content(excel_path): contents = [] if self.log_path.endswith('xls'): workbook = xlrd.o ...

  8. xamarin 遇到的奇葩问题

    未能找到路径“E:\platforms”的一部分. xamarin 安卓存档出现这样的错误 建议检查下ndk是否配置完整

  9. 剑指offer七之斐波那契数列

    一.题目 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39. 二.思路 序号:                  0  1   2   3  4   5  ...

  10. Java - replace a character at a specific index in a string?

    String are immutable in Java. You can't change them. You need to create a new string with the charac ...