php php类变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6265489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php class variables
提问by yorgos
I have a class named MyCart.
我有一个名为 MyCart 的类。
Class MyCartClass
{
var $MyCart;
function getCart(){
return $this->MyCart;
}
function addItem($item){
if ($this->MyCart){
$this->MyCart .= ','.$item;
}
else{
$this->MyCart = $item;
}
}
};
$globalCart = new MyCartClass; // create an instance of the class
The variable "$MyCart"
is a string containing all the items in the cart, separated with a comma.
该变量"$MyCart"
是一个包含购物车中所有商品的字符串,用逗号分隔。
Now, I save this class to a file named "cart.php"
and I include it in another file.
现在,我将这个类保存到一个名为的文件中,并将"cart.php"
它包含在另一个文件中。
HOWEVER, every time I call the function "addItem"
, the if statement goes to the else branch, which means that the "$MyCart"
variable does not contain the current state of the cart.
但是,每次我调用函数时"addItem"
,if 语句都会转到 else 分支,这意味着该"$MyCart"
变量不包含购物车的当前状态。
Do i need to store the state of my cart into a "session" variable? Cause this way it will be accessible from all files for sure..
我是否需要将购物车的状态存储到“会话”变量中?因为通过这种方式,它肯定可以从所有文件中访问。
I would appreciate any kind of help!
我将不胜感激任何形式的帮助!
Thanks.
谢谢。
采纳答案by dynamic
If you mean saving it between request then yes you need to put it in $_SESSION.
如果您的意思是在请求之间保存它,那么是的,您需要将它放在 $_SESSION 中。
Otherwise within one request you can use your $globalCart
and not lose the contents of your $myCart
var.
否则,在一个请求中,您可以使用您$globalCart
的$myCart
var内容而不会丢失。
回答by Daniel Quinn
Don't use a string to store a list. It's just a bad idea all around.
不要使用字符串来存储列表。这只是一个坏主意。
Instead, store $items
as an array, and define it in the constructor
相反,存储$items
为数组,并在构造函数中定义它
class Cart {
function __construct() {
$this->items = array();
}
function add($item) {
$this->items[] = $item;
}
function save() {
$SESSION["cart"] = $this->items;
}
function get_items_string() {
return join(",", $this->items);
}
}
My PHP is a little rusty, but that should get you started.
我的 PHP 有点生疏,但这应该能让你开始。
回答by Scott Keck-Warren
To simplify this just store the data in an array and use serialize and deserialize to store the data:
为了简化这一点,只需将数据存储在数组中并使用序列化和反序列化来存储数据:
Class MyCartClass {
var $MyCart = array();
public function __construct(){
$this->MyCart = deserialize($_SESSION['CART']);
}
public function __destruct(){
$_SESSION['CART'] = serialize($this->MyCart);
}
function getCart(){
return $this->MyCart;
}
function addItem($item){
$this->MyCart[] = $item;
}
}
Please note I just wrote this quickly and didn't run it as I don't have access to run it right now. :-)
请注意,我只是快速地写了这个,没有运行它,因为我现在没有权限运行它。:-)
回答by publikz.com
your can even serialise all class to $_SESSION[] ... but generally good idea is to store in session some id - as example user id & store all all other data in mysql. Getting it each time, is needed ...
您甚至可以将所有类序列化为 $_SESSION[] ...但通常好主意是在会话中存储一些 id - 例如用户 id & 将所有其他数据存储在 mysql 中。每次都得到它,需要......