On Thu, 17 Feb 2005 17:05:51 +0100, Jan Pieter Kunst
<de*****@cauce. org> wrote:
never-aways wrote: 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.
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
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.