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 ...
随机推荐
- 18_python_类关系
一.类与类之间的关系 1.依赖关系 class Elephant: def __init__(self, name): self.name = name def open(self, ...
- 修改tomcat默认端口号
修改tomcat端口号 端口修改tomcat tomcat服务器的默认端口号是8080 1 只启动一个tomcat的情况 当我们不想使用8080端口,需要修改为其他端口时,我们可以: 1, 打开tom ...
- mysql创建账号及管理权限
mysql创建账号及管理权限 0.mysql版本8.0.15,服务器版本:RHEL 6.5 1.创建用户名密码 mysql> use mysql; mysql> create user ' ...
- iOS开发笔记-图标和图片大小官方最新标准
这两天开发iOS app用到了Tab bar,然后随便切了点图标放上去发现效果极差.于是乎,开始查找苹果官方给的标准.搜索一番后,看到了一篇博文,但其内容与iOS人机交互指南最新版内容不符. 故此,在 ...
- POJ 2578
#include<iostream> #include<stdio.h> #include<vector> using namespace std; int mai ...
- 坑爹的Sun JDK
Sun的这个java.lang.Throwable 源码 设计非常糟糕,完全没有扩展性, 我在IBM 的Java JDK下,继承java.lang.Throwable重新定义了一个ExceptionW ...
- python使用(三)
1.function_option.py2.function_code_option.py3.thread_option.py4.class_option.py5.threading_option.p ...
- C# 多线程学习系列四之ThreadPool取消、超时子线程操作以及ManualResetEvent和AutoResetEvent信号量的使用
1.简介 虽然ThreadPool.Thread能开启子线程将一些任务交给子线程去承担,但是很多时候,因为某种原因,比如子线程发生异常.或者子线程的业务逻辑不符合我们的预期,那么这个时候我们必须关闭它 ...
- 微信小程序交流群,欢迎加入,其中微信小程序开发群、Jenkins开发群是有问必答群
微信小程序开发,请加群511389428,511389428 有问必答群:React开发,请加群523838207:523838207Jenkins开发,请加群155799363,155799363 ...
- Quarz.net 设置任务并行和任务串行
如何设置Quarz.net某个任务完成后再继续执行该任务? Quarz.net 的任务有并行和串行两种: 并行:一个定时任务,当执行时间到了的时候,立刻执行此任务,不管当前这个任务是否在执行中: 串 ...