LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-12-2012, 03:14 AM   #1
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Rep: Reputation: 6
"Catchable fatal error: Object of class input could not be converted to string" - php


Hello,
I'm experiencing following error in my php code:
Code:
Catchable fatal error: Object of class input could not be converted to string in /home/skyer/.../class/input.php on line 16
The source code looks like this: (line 16 is the one with preg_match()
Code:
class input
{
private $id="/^[0-9]{1,4}$/";

	public function Check_input($type,$input)
	{
		
		if (!isset($this->$type)) {if (DEBUG) echo 'input::Check_input():Cond1:variable doesn\'t exist'; return 0;}
		if (preg_match($this->$type,$input)==0) {if (DEBUG) echo "input::Check_input($this->$type,$input):Cond2:input not of expected character";return 0;}
		
	}
}
It seems to me that the problem is that PHP can't convert $this->$type into string, but I don't know why. $this->$type should be $this->id, and that's defined right above the function.

If I use $this->id directly instead of $this->$type, code works as expected.


Thank you for your help,

Skyer
 
Old 03-12-2012, 04:13 AM   #2
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: 6
Alright this is funny,
I took out the $this->$type from the string and concatenated with the rest, and it seems to work now:
Code:
if (preg_match($this->$type,$input)==0) {if (DEBUG) echo "input::Check_input(".$this->$type.",$input):Cond2:input not of expected character";return 0;}
Problem solved - sort of, but I'd still like to hear some explanation of this.



Skyer
 
Old 03-12-2012, 11:04 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
It's because you used the simple syntax for string interpolation, but it doesn't handle complex expression like $object->$property.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP "Object of class mysqli could not be converted to string " Randall Slack Programming 1 12-02-2011 10:02 AM
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
"failed to execute child process" "Input/output error" fl.bratu Fedora 4 12-15-2008 04:03 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
perl install error: Can't locate object method "new" via package "Module::Build::Vers powah Linux - Software 0 10-24-2006 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration