Passing an object property to a function - can I? [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cordie
Forum Newbie
Posts: 4
Joined: Tue Apr 25, 2006 1:17 pm

Passing an object property to a function - can I? [solved]

Post by cordie »

I'm such a newb that I don't know the right terminology for my question so searching has been fruitless. Here's what I want to do...

I have a recordset object and I pass that to a function. I also want to pass a property to the function, along with a value to test this against.

Within the function I loop through the recordset and if the test value == the object's property value on this iteration then I want to do something but I can't figure out how to pass in some kind of reference to the property I want to test against and the work around I have is plain clunky.

Here is what I have:

Code: Select all

function getNavListItems($rsRecordset, $iFieldToTest, $iValueToTest)
{
// Ya di ya...
	
	foreach ( $rsRecordset as $record )
	{
		$bTestResult = false;
		
		switch ($iFieldToTest)
		{
			case 0:
				if ( $record->pageId == $iValueToTest )
				{
					$bTestResult = true;
				}
				break;
			case 1:
				if ( $record->parentPageId == $iValueToTest )
				{
					$bTestResult = true;
				}
				break;
		}
		
		if ( $bTestResult )
		{
			// Do stuff
		}
		else
		{
			// Do other stuff
		}
	}
	
	return $sStuff;
}
Here is what I want!

Code: Select all

function getNavListItems($rsRecordset, $propertyToTest, $iValueToTest)
{
// Ya di ya...
	
	foreach ( $rsRecordset as $record )
	{
		if ( $record->$propertyToTest == $iValueToTest )
		{
			// Do stuff
		}
		else
		{
			// Do other stuff
		}
	}
	
	return $sStuff
}
Hope this all makes sense - TIA!
Last edited by cordie on Tue Apr 25, 2006 3:00 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried your "what you want" version?
cordie
Forum Newbie
Posts: 4
Joined: Tue Apr 25, 2006 1:17 pm

Post by cordie »

Yes - sorry I should have said :oops:

I take it that I have the right idea so I guess I'm passing the property incorrectly.

Here's the call:

Code: Select all

$sSecondaryNav = getLinkListItems($pageDetails, pageId, $iPageId);
And the error:

Code: Select all

Notice: Use of undefined constant pageId - assumed 'pageId' in C:\Documents and Settings\Cordie\My Documents\websites\localhost\www\test\includes\functions\getpagedetails.php on line 425
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

put quotes around pageId, you're giving the function a string. :P
cordie
Forum Newbie
Posts: 4
Joined: Tue Apr 25, 2006 1:17 pm

Post by cordie »

I could have sworn I tried that already but all is working a treat now - thankyou!
Post Reply