PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
class Test
{
function tester()
{
print "test";
}
}
$test = new Test()->tester();
?>
I was actually surprised to see that code not working. Does anyone know if I am doing something wrong and if so is there a way I can get this same effect or is it simply not possible?
The reason why I'm even trying this is because in my application I have a class that loads some external files and has a function called getContent() that returns the loaded content and I prefer to just do:
You cannot do chaining with the constructor, since it technically does not return anything. Use a static method to load the instance and return the instance, i.e. return $this;
Jcart wrote:You cannot do chaining with the constructor, since it technically does not return anything. Use a static method to load the instance and return the instance, i.e. return $this;
Ah that makes sense. I'll try the static then, thanks.
I think PHP 5.3 (or 6) is going to allow such constructs as "new Foo()->bar()";. Don't know why I'm thinking that though... I just seem to remember reading something on the internals list. It's easily worked around anyway and usually the workaround is a better approach
Chris Corbyn wrote:I think PHP 5.3 (or 6) is going to allow such constructs as "new Foo()->bar()";. Don't know why I'm thinking that though... I just seem to remember reading something on the internals list. It's easily worked around anyway and usually the workaround is a better approach
Sweet That would be really helpful for what I'm trying to do. Thanks for the tip.