return object instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • never-aways

    #1

    return object instance

    How do you return an object from a function? Take the following
    snippet...

    class Stuff
    {
    public $nNum;
    public $szText;
    }

    function MakeStuff
    {
    $oObject = new Stuff;

    $oObject->nNum = 123;
    $oObject->szText = "Hello";

    return $oObject;
    }

    function EntryPoint
    {
    $oStuff = MakeStuff(); // oStuff will equal NULL
    }

    I've tried all sorts but i just can't get an object passed out of a
    function. Its mainly useful for returning a collection of errors and
    the like. But how do you do it in php5?

    If anyone could help i would be eternally grateful.
  • Jan Pieter Kunst

    #2
    Re: return object instance

    never-aways wrote:[color=blue]
    > How do you return an object from a function? Take the following
    > snippet...
    >
    > class Stuff
    > {
    > public $nNum;
    > public $szText;
    > }
    >
    > function MakeStuff
    > {
    > $oObject = new Stuff;
    >
    > $oObject->nNum = 123;
    > $oObject->szText = "Hello";
    >
    > return $oObject;
    > }
    >
    > function EntryPoint
    > {
    > $oStuff = MakeStuff(); // oStuff will equal NULL
    > }
    >
    > I've tried all sorts but i just can't get an object passed out of a
    > function. Its mainly useful for returning a collection of errors and
    > the like. But how do you do it in php5?
    >
    > If anyone could help i would be eternally grateful.[/color]


    You need to write your function definitions like this:

    function MakeStuff() {

    }
    function EntryPoint() {

    }

    Note the parentheses.

    <?php
    class Stuff {
    public $nNum;
    public $szText;
    }

    function MakeStuff() {
    $oObject = new Stuff;
    $oObject->nNum = 123;
    $oObject->szText = "Hello";
    return $oObject;
    }

    function EntryPoint() {
    $oStuff = MakeStuff(); // oStuff will equal NULL
    var_dump($oStuf f);
    }

    EntryPoint();
    ?>

    output:

    object(Stuff)#1 (2) {
    ["nNum"]=>
    int(123)
    ["szText"]=>
    string(5) "Hello"
    }

    HTH,
    JP

    --
    Sorry, <devnull@cauce. org> is a spam trap.
    Real e-mail address unavailable. 5000+ spams per month.

    Comment

    • never-aways

      #3
      Re: return object instance

      On Thu, 17 Feb 2005 17:05:51 +0100, Jan Pieter Kunst
      <devnull@cauce. org> wrote:
      [color=blue]
      >never-aways wrote:[color=green]
      >> How do you return an object from a function? Take the following
      >> snippet...
      >>
      >> class Stuff
      >> {
      >> public $nNum;
      >> public $szText;
      >> }
      >>
      >> function MakeStuff
      >> {
      >> $oObject = new Stuff;
      >>
      >> $oObject->nNum = 123;
      >> $oObject->szText = "Hello";
      >>
      >> return $oObject;
      >> }
      >>
      >> function EntryPoint
      >> {
      >> $oStuff = MakeStuff(); // oStuff will equal NULL
      >> }
      >>
      >> I've tried all sorts but i just can't get an object passed out of a
      >> function. Its mainly useful for returning a collection of errors and
      >> the like. But how do you do it in php5?
      >>
      >> If anyone could help i would be eternally grateful.[/color]
      >
      >
      >You need to write your function definitions like this:
      >
      >function MakeStuff() {
      >
      >}
      >function EntryPoint() {
      >
      >}
      >
      >Note the parentheses.
      >
      ><?php
      >class Stuff {
      > public $nNum;
      > public $szText;
      >}
      >
      >function MakeStuff() {
      > $oObject = new Stuff;
      > $oObject->nNum = 123;
      > $oObject->szText = "Hello";
      > return $oObject;
      >}
      >
      >function EntryPoint() {
      > $oStuff = MakeStuff(); // oStuff will equal NULL
      > var_dump($oStuf f);
      >}
      >
      >EntryPoint() ;
      >?>
      >
      >output:
      >
      >object(Stuff)# 1 (2) {
      > ["nNum"]=>
      > int(123)
      > ["szText"]=>
      > string(5) "Hello"
      >}
      >
      >HTH,
      >JP[/color]


      Oops, i do usually have the parenthasis, somehow forgot to add them
      whilst composing the example.

      Also, this code seems to work when i expected it not to. Must be
      something else about my site that's messing up.

      Sorry about that. I always make comedy mistakes when starting out
      with a new language.

      Comment

      • never-aways

        #4
        Re: return object instance

        On Thu, 17 Feb 2005 15:55:01 GMT, never-aways <nospam@nospam. com>
        wrote:
        [color=blue]
        >How do you return an object from a function? Take the following
        >snippet...
        >
        >class Stuff
        >{
        > public $nNum;
        > public $szText;
        >}
        >
        >function MakeStuff
        >{
        > $oObject = new Stuff;
        >
        > $oObject->nNum = 123;
        > $oObject->szText = "Hello";
        >
        > return $oObject;
        >}
        >
        >function EntryPoint
        >{
        > $oStuff = MakeStuff(); // oStuff will equal NULL
        >}
        >
        >I've tried all sorts but i just can't get an object passed out of a
        >function. Its mainly useful for returning a collection of errors and
        >the like. But how do you do it in php5?
        >
        >If anyone could help i would be eternally grateful.[/color]

        OK, it appears to be a problem with the debugger.
        I'm using http://dd.cron.ru/dbg/ together with PHPEclipse.

        var_dump displays the correct contents, as does echoing individual
        vars. BUT, the debugger shows the returned object to be NULL.


        Comment

        Working...