致命错误:无法访问中的空属性.PEAR-PHP/PHP


Fatal error: Cannot access empty property in .. PEAR-PHP / PHP

我使用一个jQuery方法将数据传递给php方法并获得响应,这样我就可以重用返回的数据。

我得到一个错误:

DO_LicenseCount: find: CHECK autofetchd
DO_LicenseCount: find: DONE
DO_LicenseCount: FETCH: a:1:{s:12:"LicenseCount";s:2:"10";}
DO_LicenseCount: fetchrow LINE: LicenseCount = 10
DO_LicenseCount: fetchrow: LicenseCount DONE
Fatal error: Cannot access empty property in C:'PROJECTS'BRIAN'AMS'Web Application'inc'pear'DB'DataObject.php on line 3780

我使用的是WAMP堆栈-PHP 5.1.6和PearPHP。

jQuery:

<script>
function GetLicenseCount()
    {
        var SoftwareTypeFK = document.getElementById("sldSoftwareTypeList").value;
        var SoftwareNameFK = document.getElementById("SoftwareTypeList").value;
        console.log(SoftwareTypeFK);
        console.log(SoftwareNameFK);

            $.ajax({
                       type: "GET",
                       url: "/AMS_LOCAL/Modules/SoftwareLicenseAllocations/GetLicenseCount.php",
                       data: {SoftwareTypeFK: SoftwareTypeFK, SoftwareFK: SoftwareNameFK},
                       success: function(result){
                         $("txtLicenseCount").text(result);
                         console.log(result);
                       }
                     });
    };

</script>

GetLicenseCount.php:

<?php
//Local include path format
                set_include_path('C:'PROJECTS'BRIAN'AMS'Web Application;C:'PROJECTS'BRIAN'AMS'Web Application'inc'pear;C:'PROJECTS'BRIAN'AMS'Web Application'js;');
                //Search Screen
                require_once('vars.php');
                require_once('funcs.php');
        $SoftwareTypeID = $_GET['SoftwareTypeFK'];
        $SoftwareNameID = $_GET['SoftwareFK'];
        var_dump($SoftwareTypeID);
        var_dump($SoftwareNameID);
        DO_Common::DebugLevel(5);
         $LicenseCountOptions = DO_Common::toAssocArray(array(
        //$LicenseCountOptions = DO_LicenseCount::toAssocArray(array(
                'tableName' => 'LicenseCount'
                ,'selectAdd' => 'LicenseCount'
                ,'whereAdd' => 'SoftwareFK=' . $SoftwareNameID  . ' AND sldSoftwareTypeFK=' . $SoftwareTypeID
                 ));
         var_dump($LicenseCountOptions);
         echo $LicenseCountOptions;
         var_dump($LicenseCountOptions);
?>

DataObject.php的一部分:

  function toValue($col,$format = null) 
    {
        if (is_null($format)) {
            **return $this->$col;**
            echo $col;
        }
        $cols = $this->table();
        switch (true) {
            case (($cols[$col] & DB_DATAOBJECT_DATE) &&  ($cols[$col] & DB_DATAOBJECT_TIME)):
                if (!$this->$col) {
                    return '';
                }
                $guess = strtotime($this->$col);
                if ($guess != -1) {
                    return strftime($format, $guess);
                }
                // eak... - no way to validate date time otherwise...
                return $this->$col;
            case ($cols[$col] & DB_DATAOBJECT_DATE):
                if (!$this->$col) {
                    return '';
                } 
                $guess = strtotime($this->$col);
                if ($guess != -1) {
                    return strftime($format,$guess);
                }
                // try date!!!!
                require_once 'Date.php';
                $x = new Date($this->$col);
                return $x->format($format);
            case ($cols[$col] & DB_DATAOBJECT_TIME):
                if (!$this->$col) {
                    return '';
                }
                $guess = strtotime($this->$col);
                if ($guess > -1) {
                    return strftime($format, $guess);
                }
                // otherwise an error in type...
                return $this->$col;
            case ($cols[$col] &  DB_DATAOBJECT_MYSQLTIMESTAMP):
                if (!$this->$col) {
                    return '';
                }
                require_once 'Date.php';
                $x = new Date($this->$col);
                return $x->format($format);

            case ($cols[$col] &  DB_DATAOBJECT_BOOL):
                if ($cols[$col] &  DB_DATAOBJECT_STR) {
                    // it's a 't'/'f' !
                    return ($this->$col == 't');
                }
                return (bool) $this->$col;

            default:
                return sprintf($format,$this->col);
        }               
    }

有问题的行是包含在**return $this->$col;中的行(行3780)。

我在谷歌上搜索了一下,发现我应该把return $this->$col;变成return $this->col;,但这是默认情况下在PEAR包中找到的,我没有对代码做任何修改。当我更改它时,会出现许多错误。

我是不是错过了我在这里没有看到的东西?

不确定从哪里开始,因为我看不出错误中提到了哪一行,但这个

Fatal error: Cannot access empty property 

通常意味着访问对象的成员时出现错误。在这个答案中可以看到导致这种情况的一个例子。看起来你可能在做这样的事情:

<?php
class FooBar
{
    public function baz()
    {
        $col = 'someprop';
        $this->$col = 'weeee'; // There is no 'someprop' member of $this
    }
}

如果你能提供错误中引用的确切行,那可以缩小很多范围

在你的代码中,你有

function toValue($col,$format = null) 
{
    if (is_null($format)) {
        **return $this->$col;**
        echo $col;
    }
 // snip ...

echo行放在返回之前,然后执行一个骰子来停止执行,看看发生了什么。本质上,当$col的值不是$this上的属性时,脚本试图访问$this->$col,所以如果$col是字符串'something',就没有属性$this->something

删除$不是问题所在,它可能应该存在。您可以尝试修改功能,例如:

function toValue($col,$format = null) 
{
    if(is_null($format)) {
        if(isset($this->$col))
            return $this->$col;
        else
            return null; // Or false or whatever you would like
    }
    // snip...