我如何'使用$ this'解决它

问题描述 投票:0回答:1
function printPrice($price = null)
{
    $oCurrencyModel = getModel('currency');

    if(!$price && $this->price)
    {
        $price = $this->price;
    }
    return $oCurrencyModel->printPrice($price);
}

function printMileage($mileage)
{
    $oCurrencyModel = getModel('currency');

    return $oCurrencyModel->printPrice($mileage);
}

错误:

致命错误:未捕获错误:/home/masanwatch/www/modules/nproduct/nproduct.item.php:89不在对象上下文中时,使用$ this堆栈跟踪:#0 / home / masanwatch / www / files / cache /template_compiled/3a2e9f2cf85b939b1b1026ccb734f400.compiled.php(82):nproductItem :: printPrice(0)#1 /home/masanwatch/www/classes/template/TemplateHandler.class.php(430):include('/ home / masanwatc .. 。')#2 /home/masanwatch/www/classes/template/TemplateHandler.class.php(197):TemplateHandler-> _fetch('file:/// home / ma ...')#3 / home / masanwatch /www/classes/display/HTMLDisplayHandler.php(61):TemplateHandler-> compile('./ modules / ncart ...','ordercomplete.h ...')#4 / home / masanwatch / www / classes / display / DisplayHandler.class.php(64):HTMLDisplayHandler-> toDoc(Object(ncartView))#5 /home/masanwatch/www/classes/module/ModuleHandler.class.php(1153):DisplayHandler-> printContent(Object( ncartView))#6 /home/masanwatch/www/index.php(59):ModuleHandler-> displayContent(Object(ncartView))#7 {main}丢在/ home / masanwatch / www / m中odules / nproduct / nproduct.item.php,第89行

我该如何解决?

php this
1个回答
0
投票

如果您在课堂上使用函数,您可以通过$this调用它,但是如果您在另一个页面中使用它,则必须创建这样的对象

class foo
{
     function printPrice($price = null)
    {
        $oCurrencyModel = getModel('currency');

        if(!$price && $this->price)
        {
            $price = $this->price;
        }
        return $oCurrencyModel->printPrice($price);
    }

    function printMileage($mileage)
    {
        $oCurrencyModel = getModel('currency');

        return $oCurrencyModel->printPrice($mileage);
    }
}

$bar = new foo;
$mileage = null;
$bar->printMileage($mileage);
© www.soinside.com 2019 - 2024. All rights reserved.