A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearForce PHP to error on non-declared variables? In objects? - page 1
User InfoPosts
Force PHP to error on non-declared variables? In objects?#1
Is there any way to force PHP to blow up (error, whatever) if I misspell a variable name? What about if I'm using an instance of a class and I spell the name of a variable wrong?

[I know that I should just get used to it, but maybe there's a way to enforce name checking?]

Thanks!

Edit: Sorry, that wasn't very specific. Here's the code, and I would like to get two errors. Right now I only get one (for the last line).

error_reporting(E_ALL|E_STRICT);
class Joe {
public $lastName;
}

$joe = new Joe();
$joe->lastNombre = "Smith";
echo "And here it is " . $jose;

posted date: 2008-12-28 11:54:00


Re: Force PHP to error on non-declared variables? In objects?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-28 11:54:01


Re: Force PHP to error on non-declared variables? In objects?#3
You can use this in your code:error_reporting(E_ALL);or this in php.iniphp_error_reporting=5http://us2.php.net/error_reporting

posted date: 2008-12-28 11:58:00


Re: Force PHP to error on non-declared variables? In objects?#4
Setting error_reporting to include E_NOTICE may help somewhat. It has a tendency to display a notice/error whenever you use a undefined variable(note that it doesn't stop the execution).

posted date: 2008-12-28 11:59:00


Re: Force PHP to error on non-declared variables? In objects?#5
I'll be trying that in a few seconds...

posted date: 2008-12-28 12:05:00


Re: Force PHP to error on non-declared variables? In objects?#6
Yeah, I don't need it to stop execution, it's for dev, not for production, if you will...

posted date: 2008-12-28 12:27:00


Re: Force PHP to error on non-declared variables? In objects?#7
From the PHP docs on error_reporting:// Reporting E_NOTICE can be good too (to report uninitialized// variables or catch variable name misspellings ...)error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

posted date: 2008-12-28 12:44:00


Re: Force PHP to error on non-declared variables? In objects?#8
Here is something I whipped up really quickly to show how you can trigger errors when something like that happens:<?phperror_reporting( E_ALL | E_STRICT );class Joe { public $lastName; public function __set( $name, $value ) { if ( !property_exists( $this, $name ) ) { trigger_error( 'Undefined property via __set(): ' . $name, E_USER_NOTICE ); return null; } $this->$name = $value; } public function __get( $name ) { if ( !property_exists( $this, $name ) ) { trigger_error( 'Undefined property via __get(): ' . $name, E_USER_NOTICE ); return null; } return $this->$name; }}$joe = new Joe();$joe->lastNom = "Smith";echo $joe->lastNom , "\n";?>

posted date: 2008-12-28 12:55:00


Re: Force PHP to error on non-declared variables? In objects?#9
Calling property_exists() is unnecessary because the magic method wouldn't be called in the first place if the property exists

posted date: 2008-12-28 13:08:00


Re: Force PHP to error on non-declared variables? In objects?#10
I realize in this example that is the case but if you're using __set() and/or __get() for private members, the magic methods are called. shrug

posted date: 2008-12-28 13:19:00


Re: Force PHP to error on non-declared variables? In objects?#11
Your implementation of __set() is broken: $name = $value should be $this->$name = $value; (however I would actually leave that line out). Also, __get() is broken, too: return $name should be return $this->$name, but again I wouldn't actually put that in there.

posted date: 2008-12-28 13:47:00


Re: Force PHP to error on non-declared variables? In objects?#12
Thanks Nick, but I'm just trying to get error-free code, not create more! Vote up for verbosity and effort, though!

posted date: 2008-12-28 13:59:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.