D
D
dev4002016-05-17 23:02:16
PHP
dev400, 2016-05-17 23:02:16

How to return the state of an object?

Explain with examples
Simple class

class Example {

    public $variable;

    public function foo() {

        //some code

    }

}

His object
$obj = new Example();
$obj->variable = "Переменная";

Some method that takes an object of the Example class as an argument
//some code

public function method(Example $obj) {

    //some code

}

//some code

How to pass the Example object to method(Example $obj) with the state that we got after working with this object (in our case, so that the $obj->variable property already contains the value "Variable"?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2016-05-17
@Denormalization

$obj = new Example();
$obj->variable = "Переменная";

$anotherobj = new AnotherObj;
$anotherobj->method($obj);

So, no?

S
Stanislav Harakhnin, 2016-05-18
@Chupokabr

If you are not sure, you can explicitly specify the transfer of an object by reference

$obj = new Example();
$obj->variable = "Переменная";

$anotherobj = new AnotherObj;
$anotherobj->method(&$obj);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question