Bu eski bir yazı olduğunu biliyorum - ama bir çözüm arıyorsanız genelinde tökezledi.
Bu benim son çözümdür:
I have an abstract class that my classes extend that throws an error on magic __get and __set.
Bu kötü - ama bir tedavi çalışır!
Yukarıdaki önerilere benzer - ama aynı zamanda bunu üzerine yazılmasını insanları durdurmak için yöntem bildirimi için 'son' ekledik.
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);
}
}