例如,一个简单的mvc类型系统:
/ api/class/method使用.htaccess/nginx.conf重写为php变量
然后做类似的事情:
<?php
// Set up class + method variables
$className = some_class_filter($_GET['class']);
$method = some_method_filter($_GET['method']);
// Check if class exists and execute
if(file_exists(BASE . "/controllers/" . $className . ".class.php")) {
require BASE . "/controllers/" . $className . ".class.php";
$$className = new $className();
// Execute the method
$$className->$method();
} else {
// Spit out some error based on the problem
}
?>
Run Code Online (Sandbox Code Playgroud)
这是非常糟糕的做法吗?如果这是不好的做法,有人可以解释为什么?如果是这样,有没有更好的方法来做我正在做的事情?
编辑基本上我使用变量变量的原因是为了简化核心系统的扩展 - 即 - 添加一个新的控制器很简单.我绝对理解允许基本上任何函数或类在没有某种过滤器的情况下实例化的安全风险.
'some_filter_here'可以是允许的控制器列表 - 白名单,正如这里提到的那样.
我试图在C#中构建一个数据库构造函数(工厂?),但是我很困惑如何在"所有路径返回值"的同时管理错误
例如,这是一个我想用来返回数据库连接的类:
public class DB
{
static SqlConnection Connect()
{
SqlConnection thisConnection;
try {
thisConnection = new SqlConnection(connectionString);
}
catch (SqlException e) { Console.WriteLine(e.Message); }
return thisConnection;
}
}
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为"并非所有路径都返回一个值".
如何在使用方法实际返回值时使用try/catch进行管理?显然我不是一个主程序员;)但我正在研究它.
我接手一个现有程序员的项目,老实说,他把项目留在了一大堆无法维护,难以理解的混乱中(编辑/澄清:几十个独立的.php页面,这些是php/html/css所有引用一个巨大的1500行'functions.php'文件,确认)
话虽如此,似乎几乎每个地方都有变量,数组等,他使用了printf().
例如,而不是使用:
foreach($thing as $t) {
echo "<option value='".$t."'>".$t."</option>";
}
Run Code Online (Sandbox Code Playgroud)
他会使用类似的东西:
foreach($thing as $t) {
printf("<option value='%s'>%s</option>", $t, $t);
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么他会这样做?使用printf()而不是echo/print,是否有一些我不知道的未知好处?
请注意,这不仅适用于可能需要验证/清理的值,而是适用于所有情况.从数据库,随机变量和其他地方定义的数组中提取的数据,绝对一切都是printf()而不仅仅是echo或print,我试图找出他为什么会使用这种方法,因为它可能有助于我理解逻辑在他建立的一些事情背后.
例如,假设我使用工厂创建一组对象:
function factory() {
e.x = 0;
e.y = 0;
return e;
}
Run Code Online (Sandbox Code Playgroud)
如何将这些对象传递给渲染函数?
例如:
function render() {
a[0].someMethod;
}
function init() {
for(i=0;i<10;i++) {
things[i] = factory();
}
setInterval(render(things),40);
}
Run Code Online (Sandbox Code Playgroud) php ×2
variables ×2
asp.net ×1
c# ×1
class ×1
game-engine ×1
html5 ×1
javascript ×1
mysql ×1
printf ×1
setinterval ×1
validation ×1