Jump to content

Get "Attempt to assign property of non-object" Warning when assigning variable


sbsads

Recommended Posts

I get the following error when I try to pass a value to a methiod in a loop:

 

Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670

This one has me very baffled. It will work the first time, and seems to work every other time, so I have no clue what is wrong. Here is the code:

 

This code is part of my "display" class:

 

	function display_partner ($type,$loc,$rand=0,$narrow=0) {
	$this->partners = new partner($this->cxn);
	$display = '
	<div id="cont_info" class="partner-list">
			<div>
				<h3 class="settings">'.ucfirst($loc).' '.ucfirst($type).last_letter($type).'s</h3>
			</div>
			<div class="settings-value" style="height:12px;padding:0;margin:0;text-align:right;padding-right:10px;">
<a href="" class="trunc">Add your organization to this list</a></p>
			</div>
			<div style="height:2px;padding:0;margin:0;">
				<hr class="account" />
			</div>
			';
	$ids = $this->partners->get_partners_list($type,$loc,$rand);
	for ($b=0;$b<sizeof($ids->id);$b++) {
		$this->partnerID = $ids->id[$b];
		$display .= ($narrow)? $this->card_partner_narr():$this->card_partner();
		if ($b!=(sizeof($ids->id)-1)) { $display .= '<hr class="account" />'; }
	}

	if (sizeof($ids->id)==0) {
		$display .= '<div style="color:#999999;display:line;text-align:center;height:20px;">No Partners found for '.ucfirst($loc).' '.ucfirst($type).'</div>';
	}

	$display .= '
	</div>';

	return $display;

}

function card_partner () {
	$this->partners->set_partner_id($this->partnerID);
	$part_info = $this->partners->get_partner_info();
	if ($part_info) {
			$display .= '
			<table class="settings">
				<tr>
					'.$this->show_if($part_info['partLogo']['val'],'<td class="settings-value" rowspan="2"><img src="'.LOGO_FOLDER.$part_info['partLogo']['val'].'" '.resize_img(LOGO_FOLDER.$part_info['partLogo']['val'],175).'alt="'.$part_info['partName']['val'].'" /></td>').'
					<td class="settings-value" colspan="2"><h5>'.$part_info['partName']['val'].'</h5></td>
				</tr>
				<tr>
					<td class="settings-value">
					<span style="color:999999;">'.$part_info['partAddress']['val'].'<br />
					'.$part_info['partCity']['val'].', '.$part_info['partST']['val'].' '.$part_info['partZIP']['val'].'<br />
					'.$part_info['partPhone']['val'].'</span><br />
					<a href="'.$this->form->show_href($part_info['partWeb']['val']).'" target="_blank">'.$part_info['partWeb']['val'].'</a>
					</td>
					<td class="settings-value">'.$part_info['partInfo']['val'].'</td>
				</tr>
			</table>
			';
	}

	return $display;
}

 

This code is part of my "partners" class:

 

	function set_partner_id($partID) {
	echo '<p>partID: '.$partID.' '.gettype($partID).'<br>
	$this->partner->id: '.$this->partner->id.'</p>';
	$this->partner->id = $partID; ///*** ERROR HAPPENS HERE ***/
	echo '<p>id set: '.$this->partner->id.'<br>
	$this->partner->id: '.$this->partner->id.'</p><hr>';
}

function get_partner_id() {
	return $this->partner->id;
}

// gets user info at login
function get_partner_info() {
	$this->partner = $this->cxn->proc_info('partner','partID',$this->partner->id);//$this->partner->id
	return $this->partner;
}

 

The following is the output generated:

 

partID: 24 string

$this->partner->id:

id set: 24

$this->partner->id: 24

 

partID: 26 string

$this->partner->id:

Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670

id set:

$this->partner->id:

 

partID: 17 string

$this->partner->id:

id set: 17

$this->partner->id: 17

 

 

partID: 25 string

$this->partner->id:

Warning: Attempt to assign property of non-object in /Users/staceyschaller/Sites/dev_zone/ckwv2/classes/class.php on line 670

id set:

$this->partner->id:

 

As you can see, the value passes to $this->set_partner_id($partID) each time. It is formatted as a string. When it assigns the value to $this->partner->id, however, sometimes it works, and sometimes it doesn't. It's probably something obvious, but I've racked my brain to see what it is. Any ideas?

 

Link to comment
Share on other sites

I'm not sure of the specific problem because my time is limited but best practices use methods to set properties.

 

Properties should be specific to that object, they should be the variables of the object itself, What you should do is use getter and setter methods that would interact with these properties in a standard way, otherwise you could have code that could quickly get out of hand.

 

-cb-

 

Note:

$this->partnerID = $ids->id[$b]

and

$this->partners->set_partner_id($this->partnerID);

 

Are contradictory, the second one is what im talking about. This way you can check the property exists, make sure its validated properly etc all in one place.

Link to comment
Share on other sites

I'm not sure exactly what you are saying, but if I understand you croorectly, what you mentioned actually is what I am doing. :) I have a process within my display class that picks the "partners" to show based on specific criteria for the requested page. It loops through the ID's that are pulled from the query and then assigns the partnerID to the partner class through a setter method.

 

Once the partner ID property is set, I just call a method within the partner class that pulls the info from the database for that specific partner and passes the values to the display class where they are formatted for display.

 

I am baffled, however, because, while the code works just fine elsewhere in the script, in this case, the setter method works fine on, say, the first and third pass, but on the second it doesn't. I have tried a variety of possibilities.

[*]The first was to set the partner ID immediately at the point where $ids->id[$b] occurs.

[*]The second was to assign the value of $ids->id[$b] to a standard string ($partID) and call the method at a different point in the script

[*]The final is as you see, to try to make the value a property of the display object, then pass that property through the setter method to the partner class

 

All attempts have generated identical results. The first and third passes set the property effectively. The Second does not.

 

I have not tested it further, but is there some sort of conflict that is negating the value of $ids->id[$b] right at the point where I pass the value? You can see that the value passes to the setter method, but it "craps out" just as it tries to pass the value.

 

I'm really at a loss. :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.