PHP不推荐使用大括号代码 - 没有关于它的文档?

时间:2016-01-29 22:45:53

标签: php syntax php-5.6 incompatibility prado

在PHP 5.2服务器上,此代码正常运行,但在5.6下它不再起作用了:

$value = $record->{self::TABLE_METHOD}();

5.6以下我必须这样做:

$abc=self::TABLE_METHOD;
$value = $record->{$abc}();

否则说,它使用" self :: TABLE_METHOD"通过变量,但不是花括号。

对于为什么会发生这种情况的任何帮助都将受到高度赞赏!

根据要求,这是"真实"代码:

class TActiveRecordGateway extends TComponent
{
    private $_manager;
    private $_tables=array(); //table cache
    private $_meta=array(); //meta data cache.
    private $_commandBuilders=array();
    private $_currentRecord;

    /**
     * Constant name for specifying optional table name in TActiveRecord.
     */
    const TABLE_CONST='TABLE';
    /**
     * Method name for returning optional table name in in TActiveRecord
     */
    const TABLE_METHOD='table';

    /**
     * Record gateway constructor.
     * @param TActiveRecordManager $manager
     */
    public function __construct(TActiveRecordManager $manager)
    {
            $this->_manager=$manager;
    }

    /**
     * @return TActiveRecordManager record manager.
     */
    protected function getManager()
    {
            return $this->_manager;
    }
    /**
     * Gets the table name from the 'TABLE' constant of the active record
     * class if defined, otherwise use the class name as table name.
     * @param TActiveRecord active record instance
     * @return string table name for the given record class.
     */
    protected function getRecordTableName(TActiveRecord $record)
    {

            $class = new ReflectionClass($record);

            if($class->hasConstant(self::TABLE_CONST))
            {
                    $value = $class->getConstant(self::TABLE_CONST);
                    if(empty($value))
                            throw new TActiveRecordException('ar_invalid_tablename_property',
                                    get_class($record),self::TABLE_CONST);
                    return $value;
            }
            elseif ($class->hasMethod(self::TABLE_METHOD))
            {
                    $value = $record->{self::TABLE_METHOD}();
                    # THE PROBLEM HAPPENS HERE : $value is empty

0 个答案:

没有答案