Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call to member function on non object

Status
Not open for further replies.

krisc13

Programmer
Jul 17, 2006
42
US
I have a function I call that works perfect on one page and returns the fatal error "Call to member function get_type() on a non object" on another page. I am calling this function within another and my class file is included just fine as other functions from that class work on this page. The switch statement is the same on the page that works and this one...

Code:
if (is_null($cust['date_viewed'])){
//get type
switch ($cust->get_type()){
	case 'buy':
	$cust_type = 'Buying';
	break;
        case 'sell':
	$cust_type = 'Selling';
	break;
        case 'buy/sell':
	$cust_type = 'Buying & Selling';
	break;
	default:
	$cust_type = 'Unknown Type';
}

I have scoured help pages and manuals and cannot seem to solve my problem. Any suggestions would be appreciated. Thank you!
 
This piece of code:

[tt]if (is_null($cust['date_viewed'])){
//get type
switch ($cust->get_type()){[/tt]

intrigues me. In one line, your code treats $cust as though it were an array (the reference [tt]$cust['date_viewed'][/tt]). In another line, your code treats $cust as though it were an object (the reference [tt]$cust->get_type()[/tt]).



Want the best answers? Ask the best questions! TANSTAAFL!
 
That's a good question. $cust is coming from an array but is a single customer. $cust['date_viewed'] is...wait maybe it would help if I put the whole function here. I changed $cust to $cust['cust_id'] as you helped me realize that was part of the problem. However I still get the same non object error...

Code:
function make_item($params, $cust,$n, $stt) {
$Date=date("Y-m-d");
$Time=date("H:i:s"); 
$datetime=$Date."T".$Time;

$sql=mysql_query("select * from activity  where activity_date_scheduled >= '$datetime' and activity_with_id= '".$cust['cust_id']."'") or die(mysql_error());

$numpending=mysql_num_rows($sql);

$res=mysql_fetch_array($sql);

if ($numpending>0) {
     $titlePending = $numpending.' appointments pending';
     //$css_class = 'lead';}
if (!is_null($cust['date_refered_in'])) {
     $title = 'Referred by: ' . $cust['refered_by'] . ' on ' .date('n/j', $cust['date_refered_in']). '.  &nbsp'.@$titlePending;
$css_class = 'refer-in';
		}

elseif (!is_null($cust['date_refered_out'])) {
	$title = 'Referred to: ' . $cust['refered_to'] . ' on ' . date('n/j', $cust['date_refered_out']). '.   '.@$titlePending;
			$css_class = 'refer-out';
		}

elseif (is_null($cust['cust_user'])) {
     $title = 'Shared by ' .$cust['shared_by'].'   '.@$titlePending;
$css_class = 'shared';
		}
		

else {
	if (is_null($cust['date_viewed'])){
	//get type
	switch ($cust['cust_id']->get_type()){
	case 'buy':
	$cust_type = 'Buying';
	break;
	case 'sell':
	$cust_type = 'Selling';
	break;
	case 'buy/sell':
	$cust_type = 'Buying & Selling';
	break;
	default:
	$cust_type = 'Unknown Type';
	}
	$title = (is_null($cust['date_added']))
	? 'New lead added on: ' . date('n/j', $cust['date_added'])
	: 'New Lead: ' . $cust_type.'   '.@$titlePending;
	$css_class = 'lead';
	}
else
	{
	$title = (is_null($cust['date_added']))
	? 'New lead added on: ' . date('n/j',     $cust['date_added'])
	: 'Last viewed: ' . date('n/j', $cust['date_viewed']).'   '.@$titlePending;
	$css_class = 'lead';
	}
}

This function works very well without my switch statement. When I try and call the get_type() it breaks. $cust is a single customer passed into this function. I hope that makes sense.
 
At the top of your definition of make_item(), add the following code:

[tt]print '<pre>';
print_r ($cust);
print '</pre>';
die();[/tt]

then post what is output.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Okay thanks I did. It is an array as you said.

Code:
Array
(
    [cust_id] => 2371
    [priority] => 4
    [f_name] => Test
    [l_name] => Eleven
    [email] => 
    [address] => 
    [address_2] => 
    [city] => 
    [zip] => 
    [state] => 
    [group_id] => 
    [shared_by] => 
    [owner_id] => 3912
    [lead_date] => 1155047731
    [date_added] => 1155047731
    [date_viewed] => 
    [date_refered_in] => 
    [date_refered_out] => 
    [refered_by] => 
    [refered_to] => 
    [cust_user] => 3912
)

However I don't understand then why $cust['cust_id'] doesn't work. Thanks again for any input.
 
It doesn't work because as your output demonstrates and the error message attests, $cust['cust_id'] is not an object but rather an integer or string.

If $cust['cust_id'] were an object, that output would say so. For example, this test script:

Code:
<?php
class foo
{
	var $a;
	
	function foo()
	{
		$this->a = 2;
	}
}

$b = array();

$b['foo'] = 'c';
$b['bar'] = new foo();

print '<pre>';
print_r ($b);
print '</pre>';

?>

outputs:

[tt]Array
(
[foo] => c
[bar] => foo Object
(
[a] => 2
)

)[/tt]

Notice that $b['bar'] is explicitly an object. From the output you've posted, no element of $cust is an object.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks so much. I now have a variable $id which is an object and has the correct id as it's value. However now I get

Code:
Call to undefined method id::get_type()

Still tryin....
 
Good idea thanks very much for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top