0

On a PHP 5.2 server this code was working, but it doesn't work any more under 5.6 :

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

Under 5.6 I had to do that :

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

Otherwise said, it works using "self::TABLE_METHOD" via a variable, but not in curly braces.

Any help on why this is happening would be highly appreciated !

As requested, this is the "real" code :

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
ern
  • 71
  • 10
Denis BUCHER
  • 310
  • 4
  • 16
  • Any error or log message? – Dan Jan 29 '16 at 22:50
  • @dan08 no error or log message. The "$value" variable is simply empty. – Denis BUCHER Jan 29 '16 at 22:52
  • 3
    https://3v4l.org/rVZlZ – Charlotte Dunois Jan 29 '16 at 22:54
  • @Matt, yes but in the first case it's a constant and in the second case a variable, that's the only difference I see... But why ? I read all changes between PHP 5.2 and 5.6 and found nothing... – Denis BUCHER Jan 29 '16 at 22:54
  • 1
    It does work. As you can see above. – Charlotte Dunois Jan 29 '16 at 22:54
  • 1
    @DenisBUCHER Please post the whole code, you're maybe defining something wrong. – Charlotte Dunois Jan 29 '16 at 23:00
  • @CharlotteDunois on my server your code outputs an error, I have to put "class record" before "class test", not after : looks strange, do you think this is possible ? I can post the whole code but it's very long. It comes from Prado framework. – Denis BUCHER Jan 29 '16 at 23:05
  • @DenisBUCHER If you define record before test or test before record doesn't matter. Either class just have to be defined before actually using it. What is the error message you get? – Charlotte Dunois Jan 29 '16 at 23:07
  • 1
    Also does the method `table` of `$record` even return something? Echo something in the method, you'll know then if the method gets run or not. – Charlotte Dunois Jan 29 '16 at 23:09
  • If I don't put class record before class test, it says PHP Fatal error: Call to undefined method record::table() in ...test.php on line 7. I have Debian 8.3 – Denis BUCHER Jan 29 '16 at 23:12
  • @DenisBUCHER I've just tested that localhost with PHP 5.6, works absolutely fine. How did you install PHP 5.6? – Charlotte Dunois Jan 29 '16 at 23:16
  • @CharlotteDunois I installed Debian 8.3 latest version, with standard apache and php : Apache/2.4.10 PHP 5.6.17-0+deb8u1. I only added php5-pgsql module – Denis BUCHER Jan 29 '16 at 23:18
  • The sole requirement to run PRADO-based applications is a Web server supporting PHP 5.3.0 or higher...This maybe problem guys.. – ern Jan 29 '16 at 23:25
  • 1
    https://3v4l.org/CXQ39 As this cut down code snippet shows, PHP has no issues with curly brackets and even if you revert the class definitions, PHP has still no problems. This is an issue only reproduceable on your server. I would suggest doing a new clean install. – Charlotte Dunois Jan 29 '16 at 23:26
  • 1
    @CharlotteDunois php has mostly backward compability..But the framework he uses does not..Problem is related with the framework OP uses.. – ern Jan 29 '16 at 23:31
  • @CharlotteDunois: Thanks a lot for your help, it seems you're right the problem is maybe with something outside PHP, as your new code outputs a PHP Fatal error: Call to undefined method record::table() on my server. Very strange, but seems to be due either to specific Debian options or something else... – Denis BUCHER Jan 29 '16 at 23:32
  • @CharlotteDunois I have an update. In your code, I renamed "record" with "erecord" and there was no error anymore. Do you think it could have something with reserved words ? Anyway, for now I solved my problem by simply using $abc=self::TABLE_METHOD; $value = $record->{$abc}(); Thank you very much. – Denis BUCHER Jan 29 '16 at 23:58
  • 1
    `record` isn't a reserved word, if so there would be an error on every PHP installation (except maybe for PHP7 installations) - maybe it's the framework you're using. – Charlotte Dunois Jan 29 '16 at 23:59
  • Yes, it's an old framework, maybe something is wrong about the class, or the scope of something. OK at least it is now working with the workaround :-) – Denis BUCHER Jan 30 '16 at 00:02
  • 1
    @DenisBUCHER that framework actually evolved to yii :)) http://www.yiiframework.com/ – ern Jan 30 '16 at 00:09

0 Answers0