OO - Accessing method in returned class without instance variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik Andersson

    OO - Accessing method in returned class without instance variable

    Hi!

    <?
    class Foo {
    function getMessage() {
    return "foo";
    }
    }
    class Bar {
    function getFoo() {
    return new Foo();
    }
    }
    $bar = new Bar();
    echo $bar->getFoo()->getMessage() ;
    ?>

    Running this you will get a parse error on the line with:

    echo $bar->getFoo()->getMessage() ;

    In an object oriented manner I would like to argue that this expression is
    completely valid. However it won't work in PHP and it annoys me. Is there
    any other way of doing it without using a variable?
    i.e.
    $foo = $bar->getFoo();
    $foo->getMessage() ;

    The reason I don't want to use another variable is that I want to be able to
    say:

    <?=$bar->getFoo()->getMessage()?> .

    All the best,

    --
    Erik Andersson ( E.N.E IT - Australia )
    ENEIT, ENE IT, Informática, soluciones integrales, redes, telefonía, barcelona

    - Australian, high quality web hosting from AU$4.95, all features included.


  • Zurab Davitiani

    #2
    Re: OO - Accessing method in returned class without instance variable

    Erik Andersson wrote:

    <snip>[color=blue]
    > echo $bar->getFoo()->getMessage() ;[/color]
    <snip>[color=blue]
    > Running this you will get a parse error on the line with:
    >
    > echo $bar->getFoo()->getMessage() ;
    >
    > In an object oriented manner I would like to argue that this expression is
    > completely valid. However it won't work in PHP and it annoys me. Is there
    > any other way of doing it without using a variable?[/color]

    That syntax is not valid in PHP4. You may have a better luck with PHP5.
    [color=blue]
    > The reason I don't want to use another variable is that I want to be able
    > to say:
    >
    > <?=$bar->getFoo()->getMessage()?> .[/color]

    You have to use 2 statements instead of 1, like you guessed.

    Comment

    • pojo

      #3
      Re: OO - Accessing method in returned class without instance variable


      Uzytkownik "Zurab Davitiani" <agt@mindless.c om> napisal w wiadomosci
      news:MZVDb.7225 2$9J3.11965@new ssvr25.news.pro digy.com...[color=blue]
      > Vamat certification wrote:
      >
      > <snip>[color=green]
      > > echo $bar->getFoo()->getMessage() ;[/color]
      > <snip>[color=green]
      > > Running this you will get a parse error on the line with:
      > >
      > > echo $bar->getFoo()->getMessage() ;
      > >
      > > In an object oriented manner I would like to argue that this expression[/color][/color]
      is[color=blue][color=green]
      > > completely valid. However it won't work in PHP and it annoys me. Is[/color][/color]
      there[color=blue][color=green]
      > > any other way of doing it without using a variable?[/color]
      >
      > That syntax is not valid in PHP4. You may have a better luck with PHP5.
      >[color=green]
      > > The reason I don't want to use another variable is that I want to be[/color][/color]
      able[color=blue][color=green]
      > > to say:
      > >
      > > <?=$bar->getFoo()->getMessage()?> .[/color]
      >
      > You have to use 2 statements instead of 1, like you guessed.[/color]
      Shawn Wilson" <shawn@glassgia nt.com> wrote in message
      news:3FE07492.E C411FE6@glassgi ant.com...[color=blue]
      > Sticks wrote:
      > [re-ordered you post because top-posting is evil :o)][color=green]
      > > <xyzzy> wrote in message news:XfqdnbU1EP quUEKi4p2dnA@co mcast.com...[color=darkred]
      > > >
      > > > "Sticks" <sarcasmic_98@y ahoo.com> wrote in message
      > > > news:3fde9f74$1 @usenet.per.par adox.net.au...
      > > > > ok... im not quite sure how to describe my problem.
      > > > > i have a php script that runs through my entire php site and writes[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > > > > resulting output to html files. this is necessary as the nature of[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > > > > hosting available to me for this particular page prohibits me from[/color][/color][/color]
      using[color=blue][color=green][color=darkred]
      > > > > php/mysql as i would like.
      > > > >
      > > > > my script works simply by using output buffers and an include
      > > > > the relevant section of code is as follows:
      > > > >
      > > > > ob_start();
      > > > > include($source .$pagename);
      > > > > $page_buffer = ob_get_contents ();
      > > > > ob_end_clean();
      > > > >
      > > > > i was wondering if there is a way i can do this without using the[/color]
      > > include,[color=darkred]
      > > > > because the script only works when it is in the same directory as[/color][/color][/color]
      the[color=blue][color=green][color=darkred]
      > > > site.
      > > > > this arrangement does not suit me, as i would rather be able to[/color][/color][/color]
      store[color=blue][color=green]
      > > the[color=darkred]
      > > > > script in a separate folder.
      > > > >
      > > > >
      > > > >
      > > > >
      > > > >
      > > > My first thought is to create an array with your directory names that[/color][/color][/color]
      you[color=blue][color=green][color=darkred]
      > > > want to scan
      > > >
      > > > $allDirs =
      > > >[/color]
      > >[/color][/color]
      array("/usr/local/apache/htdocs","/usr/local/apache/site1/","/usr/local/apac[color=blue][color=green][color=darkred]
      > > > he/site1/htdocs") etc...
      > > >
      > > > then iterate through the array and subsequent directory files.
      > > >
      > > > while (list(,$dirName ) = each($allDirs) {
      > > > // create an array of all files
      > > > $allFiles = scandir($dirNam e);
      > > > while (list ($key,$fileName ) = each($allFiles) {
      > > > // each file within each directory
      > > > $page_buffer to $fileName stuf......
      > > > }
      > > > }
      > > >
      > > >[/color]
      > > perhaps i havent been clear enough
      > > i dont want to use includes to perform this task. i would like to get[/color][/color]
      the[color=blue][color=green]
      > > html output of a php script that is quite complicated and uses a variety[/color][/color]
      of[color=blue][color=green]
      > > includes of its own which i do not wish to rewrite unless i absolutely[/color][/color]
      have[color=blue][color=green]
      > > to. these includes are disturbed when my script resides in a different
      > > directory to the page.
      > >
      > > as it stands, the script works. it just isnt an ideal solution.
      > > i am looking for something along the lines of the exec() command, only[/color][/color]
      for[color=blue][color=green]
      > > executing php scripts.
      > >[/color]
      >
      > I'm not sure I understand your problem, but if you want to execute php[/color]
      scripts[color=blue]
      > on Linux, you can use
      >
      > /pathinfo/php -q /pathinfo/scriptname.php
      >
      > from the command line or a cron job. The "-q" suppresses HTTP headers.[/color]
      Or you[color=blue]
      > could do:
      >
      > /pathinfo/php -q /pathinfo/scriptname.php > index.html
      >
      > to send all output from that php script to a file called index.html. Be[/color]
      careful[color=blue]
      > with this, though, as it's easy to overwrite the wrong files...[/color]


      Comment

      Working...