Fatal error: Call to a member function on a non-object

Hi,

Using PHP 4.4.4, and I have this class

class CUserItem {
var $m_id;
var $m_children_arr;
function CUserItem($p_id)
{
$this->m_id = $p_id;
$this->m_children_arr = array();
} // CUserItem
function addChild($p_child) {
array_push($this->m_children_arr, $p_child);
} // addChild
function numChildren() {
return count($this->m_children_arr);
} // numChildren
} // CUserItem

I have an associative array of objects, but am getting errors trying
to call a method

$items_arr = array();
$items_arr[0] = new CUserItem(1);
$item = &$items_arr[0];
print $item->numChildren(); // produces an error

The error produced is "Fatal error: Call to a member function on a non-
object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
I need to get the object by reference because I will be manipulating
it later on. What am I doing wrong above?

Thanks, - Dave
laredotornado [ Do, 01 März 2007 17:25 ] [ ID #1644123 ]

Re: Fatal error: Call to a member function on a non-object

<laredotornado [at] zipmail.com> wrote in message
news:1172766345.255553.20170 [at] v33g2000cwv.googlegroups.com...
| Hi,
|
| Using PHP 4.4.4, and I have this class
|
| class CUserItem {
| var $m_id;
| var $m_children_arr;
| function CUserItem($p_id)
| {
| $this->m_id = $p_id;
| $this->m_children_arr = array();
| } // CUserItem
| function addChild($p_child) {
| array_push($this->m_children_arr, $p_child);
| } // addChild
| function numChildren() {
| return count($this->m_children_arr);
| } // numChildren
| } // CUserItem
|
| I have an associative array of objects, but am getting errors trying
| to call a method
|
| $items_arr = array();
| $items_arr[0] = new CUserItem(1);
| $item = &$items_arr[0];
| print $item->numChildren(); // produces an error
|
| The error produced is "Fatal error: Call to a member function on a non-
| object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
| I need to get the object by reference because I will be manipulating
| it later on. What am I doing wrong above?

print_r on $items_arr[0], then print_r on $item after it is set by ref to
$items_arr[0]. finally print_r on $item after $item is set to $items_arr[0]
by value. therein lies the difference.
Steve [ Do, 01 März 2007 18:28 ] [ ID #1644130 ]

Re: Fatal error: Call to a member function on a non-object

"Steve" <no.one [at] example.com> wrote in message
news:%2EFh.1036$yi7.432 [at] newsfe03.lga...
|
| <laredotornado [at] zipmail.com> wrote in message
| news:1172766345.255553.20170 [at] v33g2000cwv.googlegroups.com...
|| Hi,
||
|| Using PHP 4.4.4, and I have this class
||
|| class CUserItem {
|| var $m_id;
|| var $m_children_arr;
|| function CUserItem($p_id)
|| {
|| $this->m_id = $p_id;
|| $this->m_children_arr = array();
|| } // CUserItem
|| function addChild($p_child) {
|| array_push($this->m_children_arr, $p_child);
|| } // addChild
|| function numChildren() {
|| return count($this->m_children_arr);
|| } // numChildren
|| } // CUserItem
||
|| I have an associative array of objects, but am getting errors trying
|| to call a method
||
|| $items_arr = array();
|| $items_arr[0] = new CUserItem(1);
|| $item = &$items_arr[0];
|| print $item->numChildren(); // produces an error
||
|| The error produced is "Fatal error: Call to a member function on a non-
|| object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
|| I need to get the object by reference because I will be manipulating
|| it later on. What am I doing wrong above?
|
| print_r on $items_arr[0], then print_r on $item after it is set by ref to
| $items_arr[0]. finally print_r on $item after $item is set to
$items_arr[0]
| by value. therein lies the difference.


btw, i'd also go about it differently. i'd do $item = new CUserItem(1) and
then add it to the $items_arr. i know that this doesn't solve your problem,
as you still need to eventually iterate the array and successfully call
numChildren...but i must mention it all the same.
Steve [ Do, 01 März 2007 18:32 ] [ ID #1644131 ]

Re: Fatal error: Call to a member function on a non-object

On Mar 1, 11:28 am, "Steve" <no.... [at] example.com> wrote:
> <laredotorn... [at] zipmail.com> wrote in message
>
> news:1172766345.255553.20170 [at] v33g2000cwv.googlegroups.com...
> | Hi,
> |
> | Using PHP 4.4.4, and I have this class
> |
> | class CUserItem {
> | var $m_id;
> | var $m_children_arr;
> | function CUserItem($p_id)
> | {
> | $this->m_id = $p_id;
> | $this->m_children_arr = array();
> | } // CUserItem
> | function addChild($p_child) {
> | array_push($this->m_children_arr, $p_child);
> | } // addChild
> | function numChildren() {
> | return count($this->m_children_arr);
> | } // numChildren
> | } // CUserItem
> |
> | I have an associative array of objects, but am getting errors trying
> | to call a method
> |
> | $items_arr = array();
> | $items_arr[0] = new CUserItem(1);
> | $item = &$items_arr[0];
> | print $item->numChildren(); // produces an error
> |
> | The error produced is "Fatal error: Call to a member function on a non-
> | object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
> | I need to get the object by reference because I will be manipulating
> | it later on. What am I doing wrong above?
>
> print_r on $items_arr[0], then print_r on $item after it is set by ref to
> $items_arr[0]. finally print_r on $item after $item is set to $items_arr[0]
> by value. therein lies the difference.


Thanks for your reply but I don't understand what you are asking me to
do. Could you provide a code sample? I want "$item" to contain a
reference ot the created object (as opposed a new copy) but I would
then like to call a method from the object being referenced.

Thanks, - Dave
laredotornado [ Do, 01 März 2007 18:37 ] [ ID #1644132 ]

Re: Fatal error: Call to a member function on a non-object

<laredotornado [at] zipmail.com> wrote in message
news:1172770679.377598.207250 [at] z35g2000cwz.googlegroups.com.. .
| On Mar 1, 11:28 am, "Steve" <no.... [at] example.com> wrote:
| > <laredotorn... [at] zipmail.com> wrote in message
| >
| > news:1172766345.255553.20170 [at] v33g2000cwv.googlegroups.com...
| > | Hi,
| > |
| > | Using PHP 4.4.4, and I have this class
| > |
| > | class CUserItem {
| > | var $m_id;
| > | var $m_children_arr;
| > | function CUserItem($p_id)
| > | {
| > | $this->m_id = $p_id;
| > | $this->m_children_arr = array();
| > | } // CUserItem
| > | function addChild($p_child) {
| > | array_push($this->m_children_arr, $p_child);
| > | } // addChild
| > | function numChildren() {
| > | return count($this->m_children_arr);
| > | } // numChildren
| > | } // CUserItem
| > |
| > | I have an associative array of objects, but am getting errors trying
| > | to call a method
| > |
| > | $items_arr = array();
| > | $items_arr[0] = new CUserItem(1);
| > | $item = &$items_arr[0];
| > | print $item->numChildren(); // produces an error
| > |
| > | The error produced is "Fatal error: Call to a member function on a
non-
| > | object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
| > | I need to get the object by reference because I will be manipulating
| > | it later on. What am I doing wrong above?
| >
| > print_r on $items_arr[0], then print_r on $item after it is set by ref
to
| > $items_arr[0]. finally print_r on $item after $item is set to
$items_arr[0]
| > by value. therein lies the difference.
|
|
| Thanks for your reply but I don't understand what you are asking me to
| do. Could you provide a code sample? I want "$item" to contain a
| reference ot the created object (as opposed a new copy) but I would
| then like to call a method from the object being referenced.

$items_arr = array();
$items_arr[0] = new CUserItem(1);
$itemTestA = &$items_arr[0];
$itemTestB = $items_arr[0];
$test = array(
'RAW DATA' => $items_arr ,
'BY REF' => $itemTestA ,
'BY VAL' => $itemTestB
);
print_r($test);

see what the differences are. does that help?
Steve [ Do, 01 März 2007 18:58 ] [ ID #1644133 ]

Re: Fatal error: Call to a member function on a non-object

On Mar 1, 11:58 am, "Steve" <no.... [at] example.com> wrote:
> <laredotorn... [at] zipmail.com> wrote in message
>
> news:1172770679.377598.207250 [at] z35g2000cwz.googlegroups.com.. .
> | On Mar 1, 11:28 am, "Steve" <no.... [at] example.com> wrote:| > <laredotorn... [at] zipmail.com> wrote in message
>
> | >
> | >news:1172766345.255553.20170 [at] v33g2000cwv.googlegroups.com.. .
> | > | Hi,
> | > |
> | > | Using PHP 4.4.4, and I have this class
> | > |
> | > | class CUserItem {
> | > | var $m_id;
> | > | var $m_children_arr;
> | > | function CUserItem($p_id)
> | > | {
> | > | $this->m_id = $p_id;
> | > | $this->m_children_arr = array();
> | > | } // CUserItem
> | > | function addChild($p_child) {
> | > | array_push($this->m_children_arr, $p_child);
> | > | } // addChild
> | > | function numChildren() {
> | > | return count($this->m_children_arr);
> | > | } // numChildren
> | > | } // CUserItem
> | > |
> | > | I have an associative array of objects, but am getting errors trying
> | > | to call a method
> | > |
> | > | $items_arr = array();
> | > | $items_arr[0] = new CUserItem(1);
> | > | $item = &$items_arr[0];
> | > | print $item->numChildren(); // produces an error
> | > |
> | > | The error produced is "Fatal error: Call to a member function on a
> non-
> | > | object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
> | > | I need to get the object by reference because I will be manipulating
> | > | it later on. What am I doing wrong above?
> | >
> | > print_r on $items_arr[0], then print_r on $item after it is set by ref
> to
> | > $items_arr[0]. finally print_r on $item after $item is set to
> $items_arr[0]
> | > by value. therein lies the difference.
> |
> |
> | Thanks for your reply but I don't understand what you are asking me to
> | do. Could you provide a code sample? I want "$item" to contain a
> | reference ot the created object (as opposed a new copy) but I would
> | then like to call a method from the object being referenced.
>
> $items_arr = array();
> $items_arr[0] = new CUserItem(1);
> $itemTestA = &$items_arr[0];
> $itemTestB = $items_arr[0];
> $test = array(
> 'RAW DATA' => $items_arr ,
> 'BY REF' => $itemTestA ,
> 'BY VAL' => $itemTestB
> );
> print_r($test);
>
> see what the differences are. does that help?

Thanks again but I can't see any difference when I print out the
array. Here's my code

print "*****************<BR>\n";
$item1 = new CUserTOCItem(7,
"",
"ROOT",
0,
0,
1,
0);
$item2 = new CUserTOCItem(4,
"",
"Chapter 1",
0,
0,
1,
0);
$a = array();
$a[7] = &$item1;
$a[4] = $item2;
print_r($a) . "<BR>\n";
print "*****************<BR>\n";

and the output

Array ( [7] => cusertocitem Object ( [m_id] => 7 [m_parent] =>
[m_title] => ROOT [m_depth] => 0 [m_hidden] => 0 [m_open] => 1
[m_order_id] => 0 [m_children_arr] => Array ( ) ) [4] => cusertocitem
Object ( [m_id] => 4 [m_parent] => [m_title] => Chapter 1 [m_depth] =>
0 [m_hidden] => 0 [m_open] => 1 [m_order_id] => 0 [m_children_arr] =>
Array ( ) ) )

- Dave
laredotornado [ Do, 01 März 2007 22:42 ] [ ID #1644135 ]

Re: Fatal error: Call to a member function on a non-object

| Thanks again but I can't see any difference when I print out the
| array. Here's my code
|
| print "*****************<BR>\n";
| $item1 = new CUserTOCItem(7,
| "",
| "ROOT",
| 0,
| 0,
| 1,
| 0);
| $item2 = new CUserTOCItem(4,
| "",
| "Chapter 1",
| 0,
| 0,
| 1,
| 0);
| $a = array();
| $a[7] = &$item1;
| $a[4] = $item2;
| print_r($a) . "<BR>\n";
| print "*****************<BR>\n";
|
| and the output
|
| Array ( [7] => cusertocitem Object ( [m_id] => 7 [m_parent] =>
| [m_title] => ROOT [m_depth] => 0 [m_hidden] => 0 [m_open] => 1
| [m_order_id] => 0 [m_children_arr] => Array ( ) ) [4] => cusertocitem
| Object ( [m_id] => 4 [m_parent] => [m_title] => Chapter 1 [m_depth] =>
| 0 [m_hidden] => 0 [m_open] => 1 [m_order_id] => 0 [m_children_arr] =>
| Array ( ) ) )

dave, you've changed your analogy. go back to creating new obj. directly to
an array element. what was the *exact* output of the code *i* gave you to
run?
Steve [ Do, 01 März 2007 22:54 ] [ ID #1644136 ]

Re: Fatal error: Call to a member function on a non-object

laredotornado [at] zipmail.com wrote:
> Hi,
>
> Using PHP 4.4.4, and I have this class
>
> class CUserItem {
> var $m_id;
> var $m_children_arr;
> function CUserItem($p_id)
> {
> $this->m_id = $p_id;
> $this->m_children_arr = array();
> } // CUserItem
> function addChild($p_child) {
> array_push($this->m_children_arr, $p_child);
> } // addChild
> function numChildren() {
> return count($this->m_children_arr);
> } // numChildren
> } // CUserItem
>
> I have an associative array of objects, but am getting errors trying
> to call a method
>
> $items_arr = array();
> $items_arr[0] = new CUserItem(1);
> $item = &$items_arr[0];
> print $item->numChildren(); // produces an error
>
> The error produced is "Fatal error: Call to a member function on a non-
> object in /usr/local/apache2/htdocs/portal/show_toc.inc on line 77".
> I need to get the object by reference because I will be manipulating
> it later on. What am I doing wrong above?
>
> Thanks, - Dave
>

Simply add: $items_arr[0] =& new CUserItem(1);
Note the &

Hendri
Hendri Kurniawan [ Do, 01 März 2007 23:13 ] [ ID #1644137 ]

Re: Fatal error: Call to a member function on a non-object

On Mar 1, 3:54 pm, "Steve" <no.... [at] example.com> wrote:
> | Thanks again but I can't see any difference when I print out the
> | array. Here's my code
> |
> | print "*****************<BR>\n";
> | $item1 = new CUserTOCItem(7,
> | "",
> | "ROOT",
> | 0,
> | 0,
> | 1,
> | 0);
> | $item2 = new CUserTOCItem(4,
> | "",
> | "Chapter 1",
> | 0,
> | 0,
> | 1,
> | 0);
> | $a = array();
> | $a[7] = &$item1;
> | $a[4] = $item2;
> | print_r($a) . "<BR>\n";
> | print "*****************<BR>\n";
> |
> | and the output
> |
> | Array ( [7] => cusertocitem Object ( [m_id] => 7 [m_parent] =>
> | [m_title] => ROOT [m_depth] => 0 [m_hidden] => 0 [m_open] => 1
> | [m_order_id] => 0 [m_children_arr] => Array ( ) ) [4] => cusertocitem
> | Object ( [m_id] => 4 [m_parent] => [m_title] => Chapter 1 [m_depth] =>
> | 0 [m_hidden] => 0 [m_open] => 1 [m_order_id] => 0 [m_children_arr] =>
> | Array ( ) ) )
>
> dave, you've changed your analogy. go back to creating new obj. directly to
> an array element. what was the *exact* output of the code *i* gave you to
> run?

To answer your question, I got

Array ( [0] => cusertocitem Object ( [m_id] => 7 [m_parent] => 0
[m_title] => ROOT [m_depth] => 0 [m_hidden] => 0 [m_open] => 1
[m_order_id] => 0 [m_children_arr] => Array ( ) ) [1] => cusertocitem
Object ( [m_id] => 4 [m_parent] => 0 [m_title] => Chapter 1 [m_depth]
=> 0 [m_hidden] => 0 [m_open] => 1 [m_order_id] => 0 [m_children_arr]
=> Array ( ) ) ) *****************

for this code (analogous to what you suggested)

$item1 = new CUserTOCItem(7,
0,
"ROOT",
0,
0,
1,
0);
$item2 = new CUserTOCItem(4,
0,
"Chapter 1",
0,
0,
1,
0);

-
laredotornado [ Do, 01 März 2007 23:17 ] [ ID #1644138 ]
PHP » alt.php » Fatal error: Call to a member function on a non-object

Vorheriges Thema: display multiple
Nächstes Thema: Form input data