Home  >  Article  >  Backend Development  >  关于读取COOKIE的有关问题

关于读取COOKIE的有关问题

WBOY
WBOYOriginal
2016-06-13 10:01:05723browse

关于读取COOKIE的问题
看到一个程序的函数文件里有下面这个函数:

function   GetCookie($Var){
        return   $_COOKIE[$Var];
}


请教,直接用$_COOKIE[$Var]调用不就可以了,为什么还要定义一个函数,定义函数有什么好处吗?

------解决方案--------------------
这叫二次封装.
做大型项目时,把原有的函数封装到一个类里面,也许功能一成不变,但这样的好处是:

1 可以统一函数命名规范(人家对变量或函数的命名规范也许与你不同).
2 你可以自己给此方法起一个看其名之其意的名字.比如你这个例子,GetCookie肯定要比$_COOKIE看起来更加舒服.
3 使程序结构看起来更加整体化.

实力化这个类后,可以用对象对这些方法进行调用:
$obj-> xxx();
$obj-> yyy();
...

这样看来起比写出n种不同命名规范的函数看起来更加舒服一些.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn