CoolMints Posted October 5, 2006 Share Posted October 5, 2006 I know, quite a subject line :)[b]the problem:[/b]I create an object of class a:[code]class a{ getOtherObject() { $newobject = new b(); return $newobject; }}class b{ myFunction() { ... }}[/code]Now, I want to access myFunction like this:[code]$obj = new a();$obj->getOtherObject()->myFunction();[/code]This produces a [i]parse error, unexpected T_OBJECT_OPERATOR[/i].[b]How can I call the member function of the object returned by getOtherObject()?[/b][i]It works if I store the result of getOtherObject() in a variable first and access the function afterwards from the variable. But there must be a way to do this inline, like in C# or JavaScript.If getOtherObject were a variable, things would work fine too. (like $obj->getOtherObject->myFunction())[/i]Thanks!Ruben Quote Link to comment Share on other sites More sharing options...
Zane Posted October 5, 2006 Share Posted October 5, 2006 right here $newobject = new b(); return $b;you have to return the object $newObject$b doesn't existand the reason you get this errorT_OBJECT_OPERATOR.is because function aren't meant to have methods or propertiesso a -> operator on a function is unexpected to PHPyou have to store the return of getOtherObject() in another property of Class A Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 5, 2006 Share Posted October 5, 2006 I've not tried it, but would PHP allow:(FuncReturnsObj())->ObjFunc(); Quote Link to comment Share on other sites More sharing options...
Zane Posted October 5, 2006 Share Posted October 5, 2006 I don't think that it would....you'd have to say declare the same function everytime like that...resulting in a new object each time...for instance if you saidFuncReturnsObj()->b = "foo";and thenFuncReturnsObj()->b += "bar";if you tried to echoFuncReturnsObj()->b it would still be undefinedthis is one way of doing what you're talking about$obj = new a();$b = $obj->getOtherObject();$b->myFunction(); Quote Link to comment Share on other sites More sharing options...
2wasted Posted October 5, 2006 Share Posted October 5, 2006 i tryed copying mgallforever code and still the same problem:-( Quote Link to comment Share on other sites More sharing options...
CoolMints Posted October 6, 2006 Author Share Posted October 6, 2006 [quote author=zanus link=topic=110631.msg447596#msg447596 date=1160087953]right here $newobject = new b(); return $b;you have to return the object $newObject$b doesn't existand the reason you get this errorT_OBJECT_OPERATOR.is because function aren't meant to have methods or properties[/quote]No that was a mistake here in my forum post (had to come up with something quick). I really meant return $newobject. Then it doesn't work too. (corrected now in first post)BTW, the error is a [b]syntax[/b] error, not a runtime error.Functions aren't meant to have methods, but objects are, so an [i]object returned by a function [/i] should also be able to have methods. Quote Link to comment Share on other sites More sharing options...
CoolMints Posted October 6, 2006 Author Share Posted October 6, 2006 [quote author=zanus link=topic=110631.msg447626#msg447626 date=1160089694]I don't think that it would....you'd have to say declare the same function everytime like that...resulting in a new object each time...for instance if you saidFuncReturnsObj()->b = "foo";and thenFuncReturnsObj()->b += "bar";if you tried to echoFuncReturnsObj()->b it would still be undefined[/quote]Thanks for your input. But that doens't fit my idea of object-oriented programming. If a reference to an object is returned, and that object is modified, and the same reference is returned later on, the object should still be modified. What I want to do really makes sence:ex.: $dataitem->GetColumn(1)->GetName();[quote author=zanus link=topic=110631.msg447626#msg447626 date=1160089694]this is one way of doing what you're talking about$obj = new a();$b = $obj->getOtherObject();$b->myFunction();[/quote]That's the solution I am using right now. But it is stupid :) "one way of doing" => do you see other ways?I think that $obj->getOtherObject()->myFunction(); just [b]should [/b] work, but maybe in a slightly different syntax. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2006 Share Posted October 6, 2006 PHP 5.1 echoes "Hello"[code]<?phpclass a{ function getOtherObject() { $newobject = new b(); return $newobject; }}class b{ function myFunction() { echo "Hello"; }}$a = new a;$a->getOtherObject()->myFunction(); // --> Hello?>[/code]What version are you using?EDIT: Just tried with 4.3.9 --> parse error! Quote Link to comment Share on other sites More sharing options...
CoolMints Posted October 6, 2006 Author Share Posted October 6, 2006 So it is a missing feature in PHP4 then?Are there any alternatives? Quote Link to comment Share on other sites More sharing options...
Jenk Posted October 6, 2006 Share Posted October 6, 2006 This functionality is only available in PHP5+. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2006 Share Posted October 6, 2006 [quote author=CoolMints link=topic=110631.msg447886#msg447886 date=1160132582]So it is a missing feature in PHP4 then?Are there any alternatives?[/quote]The object model was completely rewritten in v5.As for alternatives, You found it already. Quote Link to comment Share on other sites More sharing options...
CoolMints Posted October 6, 2006 Author Share Posted October 6, 2006 Damn, that's a pity. And my host only offers PHP4.Are there other changes then in the object field in PHP5?Thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted October 6, 2006 Share Posted October 6, 2006 http://www.php.net/manual/en/language.oop5.php Quote Link to comment Share on other sites More sharing options...
CoolMints Posted October 6, 2006 Author Share Posted October 6, 2006 Oooooooh nice :) Well, PHP5 really contains my idea of OO then. Too bad I can't use it for my current project. Thanks to all. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.