class Item {
public $itemname;
somekeyword $error; //The correct keyword I want to find
public function insertData() {
$query = "INSERT INTO table VALUES ('".$this->itemname."')";
try {
//HERE THERE IS A NICE CODE TO EXECUTE THE QUERY AND SO...
return true; //Returns true on success
} catch(PDOExecption e) {
$this->error = $PDOObject->errorInfo();
return false; //Returns false on FAIL but the Object has a property with the error information
}
}
}
如何才能使该属性错误仅对此类方法更改?我不希望它被其他课程或文件改变。如何使PHP类属性只能通过一个类更改方法
关键字“受保护”是否可以使其可读取但不可更改?
谢谢!
题外话题,但您的代码可能会受到sql注入的影响。 – Matthew
感谢马修,是的,我只是把它编码为例子,但是在实际的代码中,我有Prepared语句和安全验证! – JuanBonnett