Ayad Mfs
Ayad Mfs

Reputation: 33

php object operator -> without instance?

I have the following code in script I implement and it does work correctly, just want to understand:

   $variable1 = function1();  // function1 is a class method, it’s file included per require_once 
   $ variable1 -> function2(); // function2 is a class method, it’s file included per require_once

Isn’t -> here an object operator? But there is no initialized instance save in $variable1.

Help appreciated

Upvotes: 2

Views: 142

Answers (1)

Naftali
Naftali

Reputation: 146310

That just means that function1() returns an object.

Hence you can use that object and it's functions.

Example:

class Test {
    function function2(){
        echo "Hi";
    }
}

function function1(){ return new Test; }


//SO:

$variable1 = function1();  
$variable1->function2(); 

Upvotes: 6

Related Questions