Home > Backend Development > PHP Tutorial > How to use __tostring()? Summary of __tostring() instance usage

How to use __tostring()? Summary of __tostring() instance usage

零下一度
Release: 2023-03-08 19:56:01
Original
4139 people have browsed it

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

1. php中__tostring()魔术方法的使用方法汇总

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

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

How to use __tostring()? Summary of __tostring() instance usage

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

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

How to use __tostring()? Summary of __tostring() instance usage

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

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

How to use __tostring()? Summary of __tostring() instance usage

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

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

How to use __tostring()? Summary of __tostring() instance usage

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

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

Introduction: When you see this name, it gives you a very high-end feeling. Yes, magic methods are indeed advanced. So, what is a magic method? In PHP, methods starting with two underscores are called "Magic methods". For example, the __construct(), __destruct (), __clone() mentioned before, and __call(),,__get(), __set(),__sleep(), __wakeup(), __toString(), __autoload(), etc., They are all magic methods.

7. Understand the hexadecimal conversion and its function in JS

How to use __tostring()? Summary of __tostring() instance usage

##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

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

How to use __tostring()? Summary of __tostring() instance usage

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:

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

Introduction: //Floating point addition 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,

10. c#Detailed explanation of formatted number code example

How to use __tostring()? Summary of __tostring() instance usage

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

[Related Q&A recommendations]:

java - Why does the input and output stream have a short time interval between two write operations? If only one read operation is needed, the two data can be read out, and sleep can be used to separate them.

javascript - JS implements number formatting problem, such as 9999999 -> 9,999,999, but how to retain the leading 0 after 01234 is formatted?

javascript - js Use of toSring() and toLocaleStirng() methods

c# - WeChat development for ios uploading pictures always shows uploading, and Android can upload successfully without any problem

java - About overriding toString() method

The above is the detailed content of How to use __tostring()? Summary of __tostring() instance usage. 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