Jump to content


Photo

Operator -> on a function that returns an object


  • Please log in to reply
13 replies to this topic

#1 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 05 October 2006 - 10:22 PM

I know, quite a subject line  :)

the problem:

I create an object of class a:


class a
{
     getOtherObject()
     {
         $newobject = new b();
         return $newobject;
   }
}

class b
{
    myFunction()
    {
      ...
    }
}



Now, I want to access myFunction like this:


$obj = new a();

$obj->getOtherObject()->myFunction();


This produces a parse error, unexpected T_OBJECT_OPERATOR.

How can I call the member function of the object returned by getOtherObject()?

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())


Thanks!

Ruben



#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 05 October 2006 - 10:39 PM

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

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 05 October 2006 - 10:53 PM

I've not tried it, but would PHP allow:
(FuncReturnsObj())->ObjFunc();
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#4 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 05 October 2006 - 11:08 PM

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();

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#5 2wasted

2wasted
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 05 October 2006 - 11:13 PM

i tryed copying mgallforever code and still the same problem:-(

#6 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 08:03 AM

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


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 syntax error, not a runtime error.

Functions aren't meant to have methods, but objects are, so an object returned by a function should also be able to have methods.

#7 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 08:11 AM

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

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();

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


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 should work, but maybe in a slightly different syntax.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 06 October 2006 - 08:30 AM

PHP 5.1 echoes "Hello"
<?php
class a
{
     function getOtherObject()
     {
         $newobject = new b();
         return $newobject;
   }
}

class b
{
    function myFunction()
    {
      echo "Hello";
    }
}
$a = new a;
$a->getOtherObject()->myFunction();         // --> Hello
?>

What version are you using?

EDIT: Just tried with 4.3.9 --> parse error!
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 11:03 AM

So it is a missing feature in PHP4 then?
Are there any alternatives?

#10 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 06 October 2006 - 12:13 PM

This functionality is only available in PHP5+.

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 06 October 2006 - 12:17 PM

So it is a missing feature in PHP4 then?
Are there any alternatives?

The object model was completely rewritten in v5.


As for alternatives, You found it already.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#12 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 05:07 PM

Damn, that's a pity. And my host only offers PHP4.

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

Thanks!

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 06 October 2006 - 05:10 PM

http://www.php.net/m...nguage.oop5.php


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 CoolMints

CoolMints
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 05:18 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users