number becomes string when passed to constructor

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
jhaiduce
Forum Newbie
Posts: 3
Joined: Sat Aug 11, 2007 2:33 pm

number becomes string when passed to constructor

Post by jhaiduce »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I created a class and passed an integer to its constructor. When I instantiate the class, I find the integer has suddenly become a string...and not the same number respresented as a string. Instead I get a piece of a long string that I used elsewhere in the program. How is this possible? Why doesn't the constructor see the integer that's passed to it?

Here's my code:

Code: Select all

if(isset($_REQUEST['page_id'])) $page_id=$_REQUEST['page_id'];

if(isset($page_id))
{
	echo "Creating new page for page_id $page_id";
	$page=new Page($page_id);
}

class Page
{
	
	function __construct($parm)
	{
		if(is_numeric($parm))
		{
			$this->page_id=$parm;
		}
		elseif(strlen($parm)>0)
		{
			echo "Looking up the page with title $parm";
			$this->title=$parm;
			$this->lookup_title();
		}
		else
		{
			$this->makenew();
		}
	}
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

what number are you passing it? and how are you passing it?
jhaiduce
Forum Newbie
Posts: 3
Joined: Sat Aug 11, 2007 2:33 pm

Post by jhaiduce »

I'm passing it a 0 (zero) at the moment but I will use other positive integers later on. The code I posted includes the method I'm using to pass the number to the constructor. I get the number from the $_REQUEST array and store it in the variable page_id, which I pass to the constructor.

John
jhaiduce
Forum Newbie
Posts: 3
Joined: Sat Aug 11, 2007 2:33 pm

Fixed my problem :)

Post by jhaiduce »

Turns out, the class was being instantiated again in another place, with a string being passed the second time. Makes sense now :-)
Post Reply