我知道这是一个简单的问题,对不起,但我还在学习OOP概念。
我想要一个在执行期间应该保留其值的类属性,我需要通过同一个类中的不同方法来获取和设置该值。
我的代码:
class incarico extends globale {
static $contatore;
// delete the product
function delete(){
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->id);
// on every delete I need to get the total number of records
if($result = $stmt->execute()){
self::$contatore = $this->conn->query("select count(*) from ". $this->table_name)->fetchColumn();
return true;
}
else{
return false;
}
}
function generaProtocollo () {
// here I need the $contatore value
error_log(self::$contatore);
return $annocorrente . self::$contatore;
}
}
当我用
删除记录时$incarico = new incarico($db);
$incarico->delete()
当我(之后)调用时,正确设置值$ contatore:
$incarico = new incarico($db);
$incarico->protocollo = $incarico->generaProtocollo();
该值为空。 我做错了什么?
谢谢你, 亚历
答案 0 :(得分:1)
试试这段代码:
class Incarico extends Globale
{
private $contatore;
// delete the product
public function delete()
{
$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->id);
return $stmt->execute();
}
public function generaProtocollo ()
{
return $annocorrente . $this->contatore;
}
public function countRows()
{
return $this->contatore = $this->conn->query("select count(*) from ". $this->table_name)->fetchColumn();
}
}
您需要做的是:
用法:
$obj = new Incranico();
$obj->delete();
echo $obj->countRows();
$obj->delete();
echo $obj->countRows();
我真的不知道你的需求是什么,但删除方法也可以使用$ id的param,这样会更清楚。
答案 1 :(得分:0)
可能是您的$stmt->execute()
或query()
返回false。请注意,$annocorrente
也永远不会设置。