强制PHP在未声明的变量上出错?在对象中?

强制PHP在未声明的变量上出错?在对象中?,php,typechecking,Php,Typechecking,如果我拼错了一个变量名,有没有办法迫使PHP崩溃(不管是什么错误)?如果我使用的是一个类的实例,并且我把一个变量的名称拼写错了怎么办 [我知道我应该习惯它,但也许有办法强制进行姓名检查?] 谢谢 编辑:对不起,这不是很具体。这是代码,我想得到两个错误。现在我只得到一个(最后一行) 您可以在代码中使用此选项: error_reporting(E_ALL); 或者在php.ini中使用这个 php_error_reporting=5 您可以尝试: error_reporting(E_ALL|E_

如果我拼错了一个变量名,有没有办法迫使PHP崩溃(不管是什么错误)?如果我使用的是一个类的实例,并且我把一个变量的名称拼写错了怎么办

[我知道我应该习惯它,但也许有办法强制进行姓名检查?]

谢谢

编辑:对不起,这不是很具体。这是代码,我想得到两个错误。现在我只得到一个(最后一行)


您可以在代码中使用此选项:

error_reporting(E_ALL);
或者在php.ini中使用这个

php_error_reporting=5
您可以尝试:

error_reporting(E_ALL|E_STRICT);

编辑:好的,我现在已经阅读了更新的问题。我想你运气不好。这在PHP中是有效的。有人说这是一项功能。:)

将错误报告设置为包含E_通知可能会有所帮助。每当您使用未定义的变量时,它往往会显示一个通知/错误(请注意,它不会停止执行)。

来自以下PHP文档:


我很快就想出了一个例子,展示了在发生类似情况时如何触发错误:

<?php

error_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";

?>

要捕获未声明的属性,可以设置u\u set()魔术方法来捕获它们

function __set($sKey, $sValue)
{
   // any setter data should be *returned* here

   // NOTE: this function will only get called if the property is not
   // publicly accessible.
   trigger_error("Non-existing property name ".$sKey, E_USER_WARNING);
}

知道这是一篇老文章,但无意中发现了它,正在寻找解决方案

这是我的最终解决方案:

我的类扩展了一个抽象类,它在magic\uu get和\uu set上抛出一个错误。

这很恶心,但它很管用

它与上面的建议类似,但也在方法声明中添加了“final”,以阻止人们覆盖它

我们还使用一个代码嗅探器来捕捉这些信息,但我想在编码时“实时”获取错误消息,而不必等待嗅探器的报告。 (人们也可以忽略嗅探报告!)

下面的示例代码

   <?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);
    }
}

是的,我不需要它来停止执行,这是为了开发,而不是为了生产,如果你愿意的话……调用属性_exists()是不必要的,因为如果属性存在,在本例中我意识到这是一种情况,那么首先不会调用magic方法,但是如果你对私有成员使用u set()和/或u get(),这些神奇的方法被称为。shrugy您的_set()实现已中断:$name=$value应该是$this->$name=$value;(不过我实际上会把这一行删掉)。另外,_get()也被破坏了:return$name应该是return$this->$name,但我也不会把它放在那里。谢谢Nick,但我只是想得到无错误的代码,而不是创建更多!尽管如此,还是投票支持冗长和努力吧@Nick,您应该明确指出,仅当访问的属性不可见或根本不存在时才调用_get/_set。例如,如果您的类中有一个私有$lastNom属性,则不会引发E_通知。然而,在您的示例中,由于触发器错误后返回null,所以您的uuu get/uuu集的return语句将永远不会执行:)我已经很久没有研究过PHP了,因此无法再快速计算它+1.现在。如果我能在这方面得到更多的反馈,我会把它评为最佳答案。
function __set($sKey, $sValue)
{
   // any setter data should be *returned* here

   // NOTE: this function will only get called if the property is not
   // publicly accessible.
   trigger_error("Non-existing property name ".$sKey, E_USER_WARNING);
}
   <?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);
    }
}