使用脚本语言时如何避免混乱?
我曾经写过一种非常强的类型语言,例如java。我需要告诉编译器我将放入什么类型的变量...例如...
public static void sayHello(String aName)
我可以确保用户将一个字符串传递给我... 但是如果我使用 php,我可以做到这一点...
function sayHello($aName)
我仍然可以调用 sayHello,但我不知道参数类型是什么...我可以让名称提供更多信息,如下所示:
function sayHelloWithString($aName)
但我可以' t 阻止用户将 int 传递给我......用户仍然可以将 int 传递给我......这可能会导致很多错误......我怎样才能阻止它?有什么想法或经验分享吗?谢谢。
I used to write a very strong type language, for example, java. I need to tell the complier what type of variable I will put in... for example...
public static void sayHello(String aName)
I can ensure that the user will pass a string to me...
But if I use php, I can do that...
function sayHello($aName)
I still can call the sayHello, but I don't know what the param type......I can let the name more informative like this:
function sayHelloWithString($aName)
But I can't stop the user pass in a int to me..... the user can still pass the int to me... ...it may cause lot of errors....How can I stop it? any ideas or experience shared? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
不阻止用户传入 int 怎么样?
在 php 中,您可以检查
is_string
,但是当然,您会错过设置了__toString
的对象,或者数字到字符串的隐式转换。如果当开发人员尝试不同的东西时,您必须让您的程序痛苦地哭泣,您可以在 PHP 的更高版本中指定一个类型(即
function foo(ObjectType $bar)...< /code>)*
在大多数松散类型语言中,您希望为主要类型设置后备:
对您接受的内容要宽松,对您发送的内容要严格。
* 类型提示不支持原始类型
How about not stopping the user from passing in an int?
In php, you could check
is_string
, but of course, you'll miss out on objects that have__toString
set, or the implicit conversion of numbers to strings.If you must make your program cry in pain when a developer tries something different, you could specify a type in the later versions of PHP (i.e.
function foo(ObjectType $bar)...
)*In most loosely typed languages, you want to set up fall-backs for the major types:
Be liberal in what you accept, be strict in what you send.
* Primitive types are not supported for type hinting
有几种方法可以解决这个问题...
1 - 要使用良好的 IDE 实现 #1,您可以像这样对函数进行 docblock:
2 - 要实现 #2,请使用
is_
方法。3 - 您不能使用上面的方法执行此操作,但是像这样的东西.. 与#2 类似,除了会抛出一个可捕获的致命错误而不是
ArgumentException
。值得注意的是,除非您正在编写公开可用的库代码,否则大部分内容都是无关紧要的。您应该知道您的方法接受什么,如果您首先尝试编写高质量的代码,您应该检查这个之前。
使用一个好的 IDE(我推荐 phpStorm 一千次以上),您可以并且应该在所有类中尽可能地使用 DocBlocks。它不仅在编写 API 和普通代码时有帮助,而且您可以用它来记录您的代码,如果您需要在 6 个月后查看代码怎么办,很可能您不会 100% 记住它:-)
此外,您可以使用文档块做更多的事情,而不仅仅是定义参数类型,查找它。
There's a few ways to deal with this...
array
and a class name.1 - To implement #1 using a good IDE, you can docblock your function as such:
2 - To implement #2 use the
is_
methods..3 - You can't do this with your method above, but something like this.. Kindof the same as #2 apart from will throw a catchable fatal error rather than
ArgumentException
.It's worth noting that most of this is pretty irrelevant unless you're writing publicly usable library code.. You should know what your methods accept, and if you're trying to write good quality code in the first place, you should be checking this before hand.
Using a good IDE (I recommend phpStorm a thousand times over) you can and should utilise DocBlocks everywhere you can for all of your classes. Not only will it help when writing APIs and normal code, but you can use it to document your code, what if you need to look at the code 6 months later, chances are you're not going to remember it 100% :-)
Additionally, there's a lot more you can do with docblocks than just define parameter types, look it up.
您可以使用以下命令检查它们传递的是否是字符串:
http://php.net/manual/en/function.is-string.php
然后提供适当的错误处理。
You can check if what they passed is a string using:
http://php.net/manual/en/function.is-string.php
Then provide appropriate error handling.
在 PHP 中,您可以使用 is_string 函数检查传递的变量是否为字符串:
希望有所帮助。
In PHP you can check whether the variable that has been passes is a string by using the is_string function:
Hope that helps.
或者/另外使用类型转换将变量转换为所需类型
https://www.php.net/manual/en/language.types.type-juggling.php
Or alternatively /additionally use Type Casting to convert the variable to the required type
https://www.php.net/manual/en/language.types.type-juggling.php
您可以选择检查以确保参数的类型正确。然而,值得考虑的是,如果不是这样,你会怎么做。如果您只是要抛出异常,那么最好假设它是正确的类型,并且当您所做的事情不允许时抛出异常。如果您不打算向已经抛出的异常/错误添加任何更有用的信息,那么首先检查它就没有多大意义。
至于向用户指示您想要什么类型,我通常坚持将其包含在变量名称中:
加上合理的文档,这意味着用户可以查看该函数并弄清楚他们应该在变量名称中传递什么内容首位。
You have the option of checking to make sure the parameter is of the right type. However, it's worth considering what you'd do if it isn't. If you're just going to throw an exception, you might be better off just assuming it's the right type and the the exception be thrown when something you do isn't allowed. If you're not going to add any more useful information to the exception/error that would already be thrown, then there's not much point in checking it in the first place.
As to giving the user an indication of what type you want, I generally stick with including it in the variable name:
That, plus reasonable documentation, will mean the user can look at the function and figure out what they should be passing in in the first place.
某些脚本语言具有可以帮助您的工具。例如,perl 中的
use strict
需要在使用之前声明每个变量。但根据定义,该语言仍然是弱类型的。有时命名约定会有所帮助。例如,我们继承了古老的 Fortran 传统,即 int 变量的名称应从 i、j、k、l、m、n 开始。现在至少在索引中使用此约定。
Some scripting languages have tools that can help you. For example
use strict
in perl requires declaration of each variable before using. But still the language is weakly typed by definition.Sometimes naming conventions help. For example we inherited from good old Fortran tradition that int variables' names should start from i, j, k, l, m, n. And this convention is used now at least for indexes.