Home > Backend Development > PHP Tutorial > Summary of how to use the __tostring() magic method in php

Summary of how to use the __tostring() magic method in php

伊谢尔伦
Release: 2023-03-08 19:54:01
Original
2951 people have browsed it

tostring()魔术方法  将一个对象当做一个字符串来使用时,会自动调用该方法,并且在该方法中,可以返回一定的字符串,以表明该对象转换为字符串之后的结果。该魔术方法比较常用。   注意:如果没有定义该方法,则对象无法当做字符串来使用!类里面未定义tostring()方法的例子:

1. __invoke()如何使用?php中的__invoke()实例用法汇总

Summary of how to use the __tostring() magic method in php

简介:tostring()魔术方法  将一个对象当做一个字符串来使用时,会自动调用该方法,并且在该方法中,可以返回一定的字符串,以表明该对象转换为字符串之后的结果。该魔术方法比较常用。   注意:如果没有定义该方法,则对象无法当做字符串来使用!类里面未定义tostring()方法的例子:<?phpini_set('display_errors', 1);class...

2. 关于(String)的详细介绍

Summary of how to use the __tostring() magic method in php

简介: 在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能。本文将对常用的转换方法进行一个总结。常用的方法有Object.toString(),(String)要转换的对象,String.valueOf(Object)等。下面对这些方法一一进行分析。方法1:采用 Object.toString()方法请看下面的例子:Object ob...

3. Java中valueOf和toString,(String)之间的区别

Summary of how to use the __tostring() magic method in php

简介:在java项目的实际开发和应用中,常常需要用到将对象转为String这一基本功能。本文将对常用的转换方法进行一个总结。常用的方法有Object.toString(),(String)要转换的对象,String.valueOf(Object)等。下面对这些方法一一进行分析。方法1:采用 Object.toString()方法请看下面的例子:Object object = getObject(

4. 魔术方法___toString()实例详解(php面向对象高级教程)

Summary of how to use the __tostring() magic method in php

简介:我们都知道,在我们实例化一个类之后,访问类的属性时,都是$实例化名称->属性名这样的格式去访问,那么你们有没有直接用echo或者print输出对象呢。有的人说,这样应该是不行的,会报错吧。没错,这样直接输出确实会报错,但是,当我们用了魔术方法__toString时就可以了。  实例分析:  

5. 魔术方法__get()实例详解(php面向对象高级教程2)

Summary of how to use the __tostring() magic method in php

简介:看到这个名字,是不是有种很高级的感觉。没错,魔术方法确实很高级。 那么,什么是魔术方法呢? 在PHP中以两个下划线开头的方法,被称为"魔术方法"(Magic methods)。比如之前讲过的__construct(), __destruct (), __clone(),以及__call(),,__get(), __set(),__sleep(), __wakeup(), __toString(), __autoload()等,都是魔术方法。

6. 了解JS中的进制转换及作用

Summary of how to use the __tostring() magic method in php

Introduction: JS base conversion is divided into binary, octal, decimal, and hexadecimal conversions. We can directly use the object.toString() to achieve: Run The following code //converts decimal to hexadecimal (10).toString(16) // =>"a"//converts octal to hexadecimal (012).toString(16) // => ;"a"//Convert hexadecimal to decimal (0x16).toString(10) // =>"22"//Convert hexadecimal to octal

7. PHP object-oriented-detailed code examples of __tostring() and __invoke()

Summary of how to use the __tostring() magic method in php

Introduction: When the __tostring() magic method uses an object as a string, it will automatically call this method, and in this method, a certain string can be returned to indicate that the object is converted into a string. result. This magic method is relatively common. Note: If this method is not defined, the object cannot be used as a string! Example of undefined __tostring() method in a class:

8. Float type addition in JS Example code sharing for subtraction, multiplication and division

Summary of how to use the __tostring() magic method in php

##Introduction: //Floating point addition operation function FloatAdd(arg1,arg2 ){ var r1,r2,m; try{r1=arg1.toString().split(.)[1].length}catch(e){r1=0} try{r2=arg2.toString().split( .)[1].length}catch(e){r2=0} m=Math.pow(10,

##9.

c#Detailed explanation of formatted number code example

Summary of how to use the __tostring() magic method in php

Introduction: int a = 12345678;//The format is sring output Label1.Text = string.Format( asdfadsf{0}adsfasdf,a);Label2.Text = asdfadsf+a.ToString()+adsfasdf;Label1.Text = string.Format(asdfadsf{0:C}adsfasdf,a);//asdf

10.

Detailed introduction to the code of 22 common methods in JavaScript arrays

Summary of how to use the __tostring() magic method in php

Introduction: As mentioned above, there are a total of 22 methods for arrays. This article divides them into object inheritance methods, array conversion methods, stack and queue methods, array sorting methods, array splicing methods, subarray creation methods, and array deletion methods. Methods, array position methods, array merge methods and array iteration methods have a total of 10 categories to introduce in detail. Object inheritance methods Array is a special object that inherits the toString(), toLocaleString() and valueOf() methods of the object Object [toString ()] The toString() method returns a string formed by concatenating the string form of each value in the array..

[Related Q&A recommendations]:

javascript - 123.toString() and var a = 123; a.toString(); Why do you get different results?

##javascript - js's parseInt(1e21, 10) Why do you get 1?

java - About overriding the toString() method

javascript - typeof (new Date()) + 1 Why is the result "string" "Indeed it is not "number"

javascript - strange way of writing string in js str = (str + '') .toString();

The above is the detailed content of Summary of how to use the __tostring() magic method in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template