Summary of how to use the __tostring () Magic method in PHP

Source: Internet
Author: User
Tags php code examples javascript array
The ToString () Magic method automatically calls the method when it is used as a string, and in that method, you can return a certain string to indicate the result of the object being converted to a string.   The Magic method is more commonly used. Note: If the method is not defined, the object cannot be used as a string! An example of the ToString () method is not defined in the class: <?phpini_set (' display_errors ', 1), Class a{public $name, public $age, public $sex, &a

1. How is __invoke () used? Summary of __invoke () instance usage in PHP

Summary: The ToString () Magic method automatically calls the method when it is used as a string, and in that method, you can return a certain string to indicate the result of the object being converted to a string.   The Magic method is more commonly used. Note: If the method is not defined, the object cannot be used as a string! An example of a ToString () method is not defined within a class: &lt;? Phpini_set (& #39;d isplay_errors& #39;, &nbsp;1); Class ...

2. A detailed description of (String)

About:&nbsp; &nbsp; &nbsp; In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string. This article will make a summary of the common conversion methods. Common methods are object.tostring (), (String) objects to be converted, string.valueof (object), and so on. Each of these methods is analyzed below. Method 1: Using the Object.ToString () method, see the following example: Object ob ...

3. Differences between valueof and ToString, (String) in Java

Summary: In the actual development and application of Java projects, it is often necessary to use the basic function of converting objects to string. This article will make a summary of the common conversion methods. Common methods are object.tostring (), (String) objects to be converted, string.valueof (object), and so on. Each of these methods is analyzed below. Method 1: Using the Object.ToString () method, see the following example: Object object = GetObject (

4. Magic Method ___tostring () Example (PHP Object-oriented Advanced tutorial)

Summary: As we all know, after we instantiate a class, accessing the properties of a class is accessed in such a format as the name of the property named-----so do you use the Echo or print output object directly? Some people say, this should be not possible, will error it. Yes, this direct output does make an error, but when we use the Magic method to __tostring it. Example Analysis: <?php

5. Magic Method __get () Example (PHP Object-oriented Advanced Tutorial 2)

Introduction: See this name, is not a very high-level feeling. Yes, the Magic method is really advanced. So, what is a magic method? The method, which begins with two underscores in PHP, is known as the "Magic Method" (Magic methods). such as __construct (), __destruct (), __clone (), and __call (), __get (), __set (), __sleep (), __wakeup (), __tostring (), __ AutoLoad () and so on, are all magic methods.

6. Understand the conversion and function of JS

Introduction: JS's binary conversion, divided into 2 binary, 8 binary, 10 binary, 16 binary between the conversion, we directly use the object. ToString () can be implemented: run the following code//10 into 16 binary. ToString (+)//= "a"// 8 binary to 16 binary (012). ToString (+)//= "A"//16 into 10 binary (0x16). ToString (+)//= "22"//16 into 8-binary

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

Introduction: __tostring () Magic method when you use an object as a string, the method is called automatically, and in that method, you can return a certain string to indicate the result of the object being converted to a string.   The Magic method is more commonly used. Note: If the method is not defined, the object cannot be used as a string! Example of __tostring () method not defined in class: <?phpini_set (' display_errors ', 1); Class a{Public

8. js in the Float type subtraction sample code share

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 # formatting Digital code examples

Description: Int a = 12345678;//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 description of the code for 22 common methods in the JavaScript array

Introduction: The preceding words array total 22 methods, this article divides it into object inheritance method, array conversion method, stack and queue method, array sorting method, array stitching method, create sub-array method, array pruning method, array position method, array merging method and arrays iteration method altogether 10 class to carry on the detailed introduction An array of object inheritance methods is a special object that inherits the ToString (), tolocalestring (), and valueof () Methods of Object Objects "ToString ()" ToString () method returns a concatenation of the strings of each value in the array:

"Related question and answer recommendation":

Javascript-123.tostring () and var a = 123; A.tostring (); Why do you get different results?

Javascript-js's parseint (1E21, 10) Why did you get 1?

Java-About overriding the ToString () method

Javascript-typeof (New Date ()) + 1 result Why "string" is not "number"

Javascript-js the strange notation of the string str = (str + "). ToString ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.