本文主要和大家分享PHP中容易犯错的点,都是平常使用php的时候总结出来的,希望能帮助到大家。
1 引用的问题
2 检测变量是否设置
三种值,三种判断变量是否存在的方法结果分别为:
立即学习“PHP免费学习笔记(深入)”;
3 直接使用函数返回的数据索引
values = new ArrayObject(); } public function &getValues() { return $this->values; } }$config = new Config();$config->getValues()['test'] = 'test';echo $config->getValues()['test'];如果你不使用 object 来存储 values ,或者不使用引用将函数的结果变成values数组的引用 ,那么可能会错
Notice: Undefined index: test in /Users/leon/Documents/workspace/test/php7.php on line 20这样会破坏对象的封装性,最好这样写
class Config{ private $values = []; public function setValue($key, $value) { $this->values[$key] = $value; } public function getValue($key) { return $this->values[$key]; } }$config = new Config();$config->setValue('testKey', 'testValue');echo $config->getValue('testKey'); // 输出『testValue』















