Jump to content

Operator -> on a function that returns an object


CoolMints

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

Link to comment
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
Link to comment
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();
Link to comment
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.
Link to comment
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.
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.