Jump to content

Archived

This topic is now archived and is closed to further replies.

CoolMints

Operator -> on a function that returns an object

Recommended Posts

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

Share this post


Link to post
Share on other sites
right here
        $newobject = new b();
        return $b;

you have to return the object $newObject
$b doesn't exist


and the reason you get this error
T_OBJECT_OPERATOR.
is because function aren't meant to have methods or properties

so a -> operator on a function is unexpected to PHP
you have to store the return of getOtherObject() in another property of Class A

Share this post


Link to post
Share on other sites
I've not tried it, but would PHP allow:
(FuncReturnsObj())->ObjFunc();

Share this post


Link to post
Share on other sites
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 said
FuncReturnsObj()->b = "foo";
and then
FuncReturnsObj()->b += "bar";

if you tried to echo
FuncReturnsObj()->b it would still be undefined

this is one way of doing what you're talking about
$obj = new a();
$b = $obj->getOtherObject();
$b->myFunction();

Share this post


Link to post
Share on other sites
[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 exist

and the reason you get this error
T_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.

Share this post


Link to post
Share on other sites
[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 said
FuncReturnsObj()->b = "foo";
and then
FuncReturnsObj()->b += "bar";

if you tried to echo
FuncReturnsObj()->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.

Share this post


Link to post
Share on other sites
PHP 5.1 echoes "Hello"
[code]
<?php
class 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!

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
Damn, that's a pity. And my host only offers PHP4.

Are there other changes then in the object field in PHP5?

Thanks!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.