Know this is an old post - but stumbled across it looking for a solution.
This is my final solution:
I have an abstract class that my classes extend that throws an error on magic __get and __set.
It's nasty - but it works a treat!
It's similar to suggestions above - but have also added 'final' to the method declaration to stop people overwriting it.
We also use a code sniffer to catch these - but I wanted to get my error messages 'live' as I coded, instead of having to wait for a report from the sniffer.
(Also people can ignore sniff reports!)
Sample code below
<?php
/**
* Abstract
* Some default Abstract functions that everything should extend
*
* @package example
* @subpackage lib
*/
/**
* Class mh_abstract
* Some default MH Abstract functions that everything should extend
*
* @package example
* @subpackage lib
*/
abstract class lib_abstract
{
/**
* Throws an error if php magic set is called
*
* @param string $key
* @param string|object $value
*
* @throws Exception When trying to set a new class property
*/
final public function __set($key, $value)
{
// get if the thing we are trying to set is a object
if(is_object($value))
{
// if so let's just report it's name, not the full object
$value = 'object:'.get_class($value);
}
// throw the error!
throw new mh_lib_error_dev_unknownClassProperty('Tried to set new property '.$key.' with value '.$value);
}
/**
* Throws an error if php magic get is called
*
* @param string $key
*
* @throws Exception When trying to set a new class property
*/
final public function __get($key)
{
// throw the error!
throw new mh_lib_error_dev_unknownClassProperty('Tried to get new property '.$key);
}
}