Can I pass an object reference as a function argument? eg:

$a = new Smarty(); foo($a); // передаем ссылку на объект чтобы использовать его методы внутри функции //и в последствии передать эту ссылку другой функции. 

    2 answers 2

    Yes, that's right, you can do that.

     class A { public function test(B $b) { $b->testMethod(); } } class B { public function testMethod() { echo 'I\'m from class B<br/>'; } } function testFunction(B $b) { $b->testMethod(); } $a = new A(); $b = new B(); $a->test($b); testFunction($b); 

    both will derive I'm from class B

    • And how do you imagine the transfer of an object not by reference? - Dmitriy Simushev February
    • @DmitriySimushev and I wrote that you can not follow the link? - Alexey Shimansky
    • " So if you want, do it. " - as if hinting that there is another way;) - Dmitriy Simushev
    • @DmitriySimushev is not, it hints that if a person wants to pass an object as an argument, let it pass. I did not have enough characters to send a response. - Alexey Shimansky
    • Thanks for answers. The question arose as a result of incomprehensible errors in the code, as if hinting that I am passing an object, and the string is transmitted as a result. Rewrote, tested, everything seems fine. - Dmitriy

    In PHP, objects are passed by reference . So " passing an object reference as a function argument " is not only the right path , but the only one .