What is __ method __ in php?

Let me clarify your doubt..

  • First I will explain about CLASS & OBJECT[what is the difference..]

  • Then I will clarify about FUNCTION & METHOD[what is the difference..]

talking about Class :: It is a your logical implementation..

object :: It is a instance of your class [instance ~ COPY ~ clone ~ ...]

when you are using --NEW-- operator ---> you are creating an OBJECT [nothing but the COPY] OF YOUR CLASS.

CLASS =============> ~NEW~ =======> OBJECT [New Copy of your class..] similarly

FUNCTION ==========> ~NEW~ =======> METHOD [Belongs to Object not Class]

Class is the program you are writing while object is the copy of your class that is executing..

class resides in ROM[permanent] where as object resides in RAM[temporary memory area..]

& when you are declaring an function as static it belongs only to class not to object & you can call the same[static function] by using ClassName::staticFunctionName[]

That is why there is no existence of STATIC_METHOD Rather STATIC_FUNCTION !!!

code example::



__toString[]

public __toString[]: string

The __toString[] method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print.

Warning

As of PHP 8.0.0, the return value follows standard PHP type semantics, meaning it will be coerced into a string if possible and if strict typing is disabled.

As of PHP 8.0.0, any class that contains a __toString[] method will also implicitly implement the Stringable interface, and will thus pass type checks for that interface. Explicitly implementing the interface anyway is recommended.

In PHP 7.4, the returned value must be a string, otherwise an Error is thrown.

Prior to PHP 7.4.0, the returned value must be a string, otherwise a fatal E_RECOVERABLE_ERROR is emitted.

Warning

It was not possible to throw an exception from within a __toString[] method prior to PHP 7.4.0. Doing so will result in a fatal error.

Example #3 Simple example

The above example will output:

__invoke[]

__invoke[ ...$values]: mixed

The __invoke[] method is called when a script tries to call an object as a function.

Example #4 Using __invoke[]

The above example will output:

Example #5 Using __invoke[]

The above example will output:

Array
[
    [0] => Array
        [
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        ]

    [1] => Array
        [
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        ]

    [2] => Array
        [
            [id] => 1
            [first_name] => John
            [last_name] => Do
        ]

]
Array
[
    [0] => Array
        [
            [id] => 1
            [first_name] => John
            [last_name] => Do
        ]

    [1] => Array
        [
            [id] => 2
            [first_name] => Bob
            [last_name] => Filipe
        ]

    [2] => Array
        [
            [id] => 3
            [first_name] => Alice
            [last_name] => Gustav
        ]

]

__set_state[]

static __set_state[array $properties]: object

This static method is called for classes exported by var_export[].

The only parameter of this method is an array containing exported properties in the form ['property' => value, ...].

Example #6 Using __set_state[]

The above example will output:

string[60] "A::__set_state[array[
   'var1' => 5,
   'var2' => 'foo',
]]"
object[A]#2 [2] {
  ["var1"]=>
  int[5]
  ["var2"]=>
  string[3] "foo"
}

Note: When exporting an object, var_export[] does not check whether __set_state[] is implemented by the object's class, so re-importing objects will result in an Error exception, if __set_state[] is not implemented. Particularly, this affects some internal classes. It is the responsibility of the programmer to verify that only objects will be re-imported, whose class implements __set_state[].

__debugInfo[]

__debugInfo[]: array

This method is called by var_dump[] when dumping an object to get the properties that should be shown. If the method isn't defined on an object, then all public, protected and private properties will be shown.

Example #7 Using __debugInfo[]

The above example will output:

object[C]#1 [1] {
  ["propSquared"]=>
  int[1764]
}

jon at webignition dot net

13 years ago

The __toString[] method is extremely useful for converting class attribute names and values into common string representations of data [of which there are many choices]. I mention this as previous references to __toString[] refer only to debugging uses.

I have previously used the __toString[] method in the following ways:

- representing a data-holding object as:
   - XML
   - raw POST data
   - a GET query string
   - header name:value pairs

- representing a custom mail object as an actual email [headers then body, all correctly represented]

When creating a class, consider what possible standard string representations are available and, of those, which would be the most relevant with respect to the purpose of the class.

Being able to represent data-holding objects in standardised string forms makes it much easier for your internal representations of data to be shared in an interoperable way with other applications.

jsnell at e-normous dot com

13 years ago

Be very careful to define __set_state[] in classes which inherit from a parent using it, as the static __set_state[] call will be called for any children.  If you are not careful, you will end up with an object of the wrong type.  Here is an example:

ctamayo at sitecrafting dot com

2 years ago

Due to a bug in PHP

Bug report: //bugs.php.net/bug.php?id=69264

kguest at php dot net

5 years ago

__debugInfo  is also utilised when calling print_r on an object:

$ cat test.php


If a Variable is already set, the __set Magic Method already wont appear!

My first solution was to use a Caller Class.
With that, i ever knew which Module i currently use!
But who needs it... :]
There are quiet better solutions for this...
Here's the Code:



Outputs something Like:

Constructor will have no Module Information... Use __init[] instead!
-->  Guestbook

dhuseby domain getback tld com

14 years ago

The above hint for using array_keys[[array]$obj] got me investigating how to get __sleep to really work with object hierarchies.

With PHP 5.2.3, If you want to serialize an object that is part of an object hierarchy and you want to selectively serialize members [public, private, and protected] by manually specifying the array of members, there are a few simple rules for naming members that you must follow:

1. public members should be named using just their member name, like so:



2. protected members should be named using "\0" . "*" . "\0" . member name, like so:



3. private members should be named using "\0" . class name . "\0" . member name, like so:



So with this information let us serialize a class hierarchy correctly:

Chủ Đề