Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP类get方法始终返回0_Php - Fatal编程技术网

PHP类get方法始终返回0

PHP类get方法始终返回0,php,Php,我无法解释为什么此脚本总是返回0。如果我将其更改为echo getSKU(),它会工作,但数量、价格或名称似乎永远不会工作。如果有人有任何想法请,请帮助这是刺激我的生活 <?php session_start(); $sku = "0001"; if (!isset($_SESSION[$sku])) { $_SESSION[$sku] = new product($sku, 5); } else { } echo $_SESSION[$sku]->getQuanti

我无法解释为什么此脚本总是返回0。如果我将其更改为echo getSKU(),它会工作,但数量、价格或名称似乎永远不会工作。如果有人有任何想法请,请帮助这是刺激我的生活

<?php

session_start();

$sku = "0001";
if (!isset($_SESSION[$sku])) {
    $_SESSION[$sku] = new product($sku, 5);
} else {

}

echo $_SESSION[$sku]->getQuantity();

class product {

    var $sku;
    var $name;
    var $price;
    var $quantity;

    function __construct($par1, $par2) {
        $this->sku = $par1;
        $this->quantity = $par2;
    }

    function setSKU($x) {
        $this->sku = $x;
    }

    function getSKU() {
        echo $this->sku;
    }

    function setName($x) {
        $this->name = $x;
    }

    function getName() {
        echo $this->name;
    }

    function setPrice($x) {
        $this->price = $x;
    }

    function getPrice() {
        echo $this->price;
    }

    function setQuantity($x) {
        $this->quantity = $x;
    }

    function incrementQuantity() {
        $this->quantity++;
    }

    function getQuantity() {
        echo $this->quantity;
    }

}

您应该使用
return
而不是
echo
。您的
get…
-方法当前不返回某些内容(只是隐式地
null
),它们只是
回显您想要返回的值

要解决此问题,只需在每个
get…
-方法
echo
中替换为
return
-即

function getQuantity() {
    return $this->quantity;
}
除此之外,您应该知道,您不能将对象存储在
$\u SESSION
(实际上您可以,但是您必须实现神奇的
\u sleep
\u wakeup
-方法..)。
您应该考虑在会话中存储产品的其他解决方案(即序列化产品)

您应该使用
return
而不是
echo
。您的
get…
-方法当前不返回某些内容(只是隐式地
null
),它们只是
回显您想要返回的值

要解决此问题,只需在每个
get…
-方法
echo
中替换为
return
-即

function getQuantity() {
    return $this->quantity;
}
除此之外,您应该知道,您不能将对象存储在
$\u SESSION
(实际上您可以,但是您必须实现神奇的
\u sleep
\u wakeup
-方法..)。
您应该考虑在会话中存储产品的其他解决方案(即序列化产品)

您不应该在get方法中回显您的属性

 echo $this->Variable;
你应该随时归还它们

return $this->Variable;
将程序控制返回给调用模块。处决 在被调用模块调用后的表达式处恢复


有关返回的更多信息,请查看文档

您不应该在get methodes中回显您的属性

 echo $this->Variable;
你应该随时归还它们

return $this->Variable;
将程序控制返回给调用模块。处决 在被调用模块调用后的表达式处恢复


有关退货的更多信息,请查看文档

,而其他答案中提出的问题肯定应该得到解决,为了回答您的问题,我相信数量可能没有设定。你能试着加上这一行吗

 $_SESSION[$sku]->setQuantity(5);
 $_SESSION[$sku]->getQuantity();

虽然其他答案中提出的问题肯定应该得到解决,但为了回答您的问题,我相信数量可能没有确定。你能试着加上这一行吗

 $_SESSION[$sku]->setQuantity(5);
 $_SESSION[$sku]->getQuantity();

您不需要
var
。您应该设置类内变量的值,例如
$sku=0
@Martin我用
$\u会话运行了代码,但它运行得很好。您的代码运行得很好@ChinLeung是的,我发现即使使用
$\u会话
代码仍能正常工作。您不需要
var
。您应该设置类内变量的值,例如
$sku=0
@Martin我用
$\u会话运行了代码,但它运行得很好。您的代码运行得很好@ChinLeung是的,我发现即使使用了
$\u SESSION
,代码仍能按预期工作。代码的改进并不能解决问题。代码的改进并不能解决问题。