php开发应用程序,php – 开发强大的应用程序

博客探讨了在PHP编程中是否应该频繁使用条件检查,如isset(), is_int()等,以确保代码安全。作者通过一个夸张的示例展示了过度检查可能导致的复杂性,并寻求一个平衡点。文章提出了疑问:当检查变得冗余时,是否仍有必要进行,以及是否存在一种‘黄金法则’来指导这种做法。
摘要由CSDN通过智能技术生成

我注意到有一些函数,如is_int()或isset()或file_exists()或functions_exists(),它们在某种程度上非常有用.当我编写一些代码时,我总是会想到可能发生在我网站上的所有不好的事情,但有时候我遇到的问题包括:

Wait, this variable is set inside the PHP file; this means that no one could ever edit it, right? And if this “user” could edit it I would have a lot of more troubles because it would be able to manage the PHP file.

要么

Does this really worth something to keep checking for file that should exists always?

让我们考虑下面的例子,它本身没有意义,但会帮助我让你理解我在说什么.

PS:我故意夸大了代码.

config.php文件

$doActions = true;

的functions.php

function getID() {

return $_COOKIE['userid'];

}

class eye {

public static function see() {

// gain the object the user is looking at

return $object;

}

}

的index.php

class viewer {

private $poniesSeen = 0;

public function __construct() {

/* Magic ponies are created here */

}

public function sawAPony($id) {

if (file_exists('config.php')) {

if (isset($doActions)) {

if (is_bool($doActions)) {

if ($doActions) {

if (file_exists('functions.php')) {

if (function_exists('getID')) {

$id = getID();

if (!empty($id)) {

if (!is_int($id)) {

settype($id, 'int');

}

if (class_exists('eye')) {

if (method_exists('eye', 'see')) {

$o = eye::see();

if (is_string($o)) {

if ($o = 'pony') {

if (isset($this->poniesSeen) and is_int($this->poniesSeen)) {

++$this->poniesSeen;

return true;

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

} else {

return false;

}

}

}

现在,我想,应该保留哪些条件以及因为它们根本没有意义而抛弃的东西?我为什么不检查它们,我为什么要这样做?关于这种痴迷有没有一个黄金法则?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值