区分 PHP 中可以采用文件内容或文件路径的参数
我正在实现一个名为 Attach 的函数。我正在寻找一种方法来获取参数,该参数可以是文件路径或文件内容。这就是我所拥有的:
/**
* @param name - name of the file
* @param file - either a file or a file path
*/
function attach($name, $file) {
$attachment = array();
$attachment['name'] = $name;
if(map($file)) {
$attachment['filepath'] = $file;
$attachment['file'] = file_get_contents($file);
} else {
$attachment['file'] = $file;
$attachment['filepath'] = getcwd();
}
}
/**
* @param filepath - can take multiple forms
* ie. ui:form:text.css => ui/form/text.css
* text.css => getcwd().'text.css'
* /ui/form/text.css => /ui/form/text.css
*
* @return if file exists - return file path
* if not found - return false
*/
function map($filepath) {
// ... too long to post
}
map 函数允许您将名称空间(使用“:”)转换为文件路径。
我担心的问题是,如果文件路径中出现错误(即有人输入了错误的文件路径),我不希望它认为由于该文件不存在,所以它一定是文件内容
另外:如果可能的话,我宁愿不编辑 map()
因为它需要我更改一堆代码 - 将地图视为黑匣子。
最后:我很快就把这个例子放在一起 - 所以请不要讨论 getcwd() 的缺点和其他语法问题。我在 map()
中有一个更复杂的系统,
谢谢! 马特·穆勒
I'm implementing a function called attach. I'm looking for a way to take a param that is either a file path or a file's contents. Here's what I have:
/**
* @param name - name of the file
* @param file - either a file or a file path
*/
function attach($name, $file) {
$attachment = array();
$attachment['name'] = $name;
if(map($file)) {
$attachment['filepath'] = $file;
$attachment['file'] = file_get_contents($file);
} else {
$attachment['file'] = $file;
$attachment['filepath'] = getcwd();
}
}
/**
* @param filepath - can take multiple forms
* ie. ui:form:text.css => ui/form/text.css
* text.css => getcwd().'text.css'
* /ui/form/text.css => /ui/form/text.css
*
* @return if file exists - return file path
* if not found - return false
*/
function map($filepath) {
// ... too long to post
}
The map function allows you to turn namespaces (using ":") into filepaths.
The issue I'm worried about is if an error is made in the filepath (ie. someone types in the file path wrong) I don't want it to think that since the file doesn't exist, it must be file contents
Also: if possible, i'd rather not edit map()
as it would require me to change a bunch of code - consider map as a black box.
Finally: I put this example together quickly - so please do not discuss the shortcomings of getcwd()
, and other syntactical issues. I have a more elaborate system in place in map()
Thanks!
Matt Mueller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使 PHP 对方法重载有更好的支持,您也会遇到困难,因为两个 var(文件路径和文件内容)可能都是字符串。是什么阻止您只创建几个包装器方法,例如 Attach_filepath(..)、attach_filecontents(..)?或者,如果您选择使用一种方法,则可以添加第三个参数,例如:
我同意尝试根据具有相同值的 var 的内容来猜测用户的意图可能是一个坏主意在这两种情况下都键入。
Even if PHP had better support for method overloading, you'd have a hard time here since both vars (filepath and file contents) would probably be a string. What is preventing you from just creating a couple of wrapper methods, like attach_filepath(..), attach_filecontents(..)? Or, if you are set on having one method, you could add a third param, like:
I agree that it would probably be a bad idea to try to guess the users' intentions based on the contents of a var that would have the same type in both cases.
在添加附件之前,您的代码可以使用像
file_exists
这样的函数来确定看起来像路径的实际上是否是路径以及文件是否确实存在。此外,您可能想检查路径是否引用文件夹或文件。Before adding the attachment your code could use a function like
file_exists
to determine if what seems like a path actually is a path and the file does in fact exist. Additionally you may want to check if the path refers to a folder or a file.