php魔术方法__tostring的应用
当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();
- ?>
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的应用的更多相关文章
- PHP魔术方法__tostring()篇
下面是关于__tostring 的运用 header('Content-type:text/html;charset="utf-8"'); /*存在内置方法_tostring()的 ...
- php类知识---魔术方法__toString,__call,__debugInfo
<?php class mycoach{ public function __construct($name,$age) { $this->name = $name; $this-> ...
- PHP面向对象魔术方法之__toString函数
l 基本介绍: 当我们希望将一个对象当做字符串来输出时,就会触发__toString魔术方法. <?php header('content-type:text/html;charset=utf- ...
- php魔术方法使用场景
php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...
- PHP面向对象常见的关键字和魔术方法
在PHP5的面向对象程序设计中提供了一些常见的关键字,用来修饰类.成员属性或成员方法,使他们具有特定的功能,例如final.static.const等关键字.还有一些比较实用的魔术方法,用来提高类或对 ...
- PHP魔术方法小结.md
说明 魔术方法就是在特定场景下不需要调用而自动执行的方法.因为有魔术方法,所以我们的类可以写得很灵活~ __construct #构造方法,在类被实例化时自动调用,一般用于初始化操作; __destr ...
- PHP中的魔术方法:__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, __toString, __set_state, __clone and __autoload
1.__get.__set 这两个方法是为在类和他们的父类中没有声明的属性而设计的: __get( $property ) 当调用一个未定义的属性时访问此方法: __set( $property, $ ...
- PHP 魔术方法 __clone __toString(五)
__clone() - 当对象克隆的时候自动加载此方法 __toString() - 当对象需要echo打印输出的时候自动加载此方法 __clone() <?php class example{ ...
- PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload
1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...
随机推荐
- Poetry
1. Absence to love is what wind is to fire. It extinguishes the small; It inflames the great. 2. It ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- Git-遇到的问题以及解决方法
1.将本地内容推送到远程仓库后,远程仓库里的文件夹不可点击 原因:在本地添加文件夹A时,又在A里使用了git init命令 解决:删除文件夹A,再重新添加过 2.其他人推送不了内容到远程仓库 原因:权 ...
- iPhone X Web 设计
原文地址:https://webkit.org/blog/7929/designing-websites-for-iphone-x/ 开箱即用(开发者无需进行任何设置),在iPhone X中,Safa ...
- vue教程2-03 vue计算属性的使用 computed
vue教程2-03 vue计算属性的使用 computed computed:{ b:function(){ //默认调用get return 值 } } ---------------------- ...
- 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 ...
- android studio 一直卡在Gradle:Build Running的解决办法
转:android studio 一直卡在Gradle:Build Running的解决办法 在使用AS开发安卓应用程序的时候经常会遇到Gradle build running一直在运行甚至卡死的 ...
- 【转载】win7搜索文件怎么搜索文件名中带圆括号的文件
System.FileName:~=“(” 这样就行. 括号上加个引号 ~= 是包含的意思, ~< 是以什么为开头, = 是以什么为名, ~! 是不包含 来自: http://www.lao8. ...
- 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
- CentOS 6.7 下 PostgreSQL 9.5 的安装与配置
#yum方式安装(不同的系统版本对应的版本也不同) yum install postgresql-server #安装指定版本 yum install https://download.postgre ...