使用脚本语言时如何避免混乱?

发布于 2024-11-16 10:59:02 字数 453 浏览 0 评论 0原文

我曾经写过一种非常强的类型语言,例如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

柠栀 2024-11-23 10:59:02

阻止用户传入 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:

  • number
  • array
  • string
  • generic object

Be liberal in what you accept, be strict in what you send.

* Primitive types are not supported for type hinting

猫烠⑼条掵仅有一顆心 2024-11-23 10:59:02

有几种方法可以解决这个问题...

  1. 使用支持文档块的 IDE。这涉及编写代码时的运行前类型检查。
  2. 在函数内使用类型检查这仅有助于运行时类型检查,并且您在编写代码时不会知道。
  3. 根据类型,您可以使用内置类型提示。然而,这只适用于非标量值,特别是数组和类名。

1 - 要使用良好的 IDE 实现 #1,您可以像这样对函数进行 docblock:

/**
 * Say hello to someone.
 *
 * @param string $aName
 **/
public function sayHello($aName) {

2 - 要实现 #2,请使用 is_ 方法。

public function sayHello($aName) {
    if (!is_string($aName)) {
        throw new ArgumentException("Type not correct.");
    }
    // Normal execution

3 - 您不能使用上面的方法执行此操作,但是像这样的东西.. 与#2 类似,除了会抛出一个可捕获的致命错误而不是ArgumentException

public function manipulateArray(array $anArray) {

值得注意的是,除非您正在编写公开可用的库代码,否则大部分内容都是无关紧要的。您应该知道您的方法接受什么,如果您首先尝试编写高质量的代码,您应该检查这个之前。

使用一个好的 IDE(我推荐 phpStorm 一千次以上),您可以并且应该在所有类中尽可能地使用 DocBlocks。它不仅在编写 API 和普通代码时有帮助,而且您可以用它来记录您的代码,如果您需要在 6 个月后查看代码怎么办,很可能您不会 100% 记住它:-)

此外,您可以使用文档块做更多的事情,而不仅仅是定义参数类型,查找它。

There's a few ways to deal with this...

  1. Use an IDE that supports docblocks. This deals with the pre-runtime type checking when writing code.
  2. Use type checking within your function This only helps with the runtime type checking, and you won't know when writing your code.
  3. Depending on the type you can use built-in type hinting. This however only works for non-scalar values, specifically array and a class name.

1 - To implement #1 using a good IDE, you can docblock your function as such:

/**
 * Say hello to someone.
 *
 * @param string $aName
 **/
public function sayHello($aName) {

2 - To implement #2 use the is_ methods..

public function sayHello($aName) {
    if (!is_string($aName)) {
        throw new ArgumentException("Type not correct.");
    }
    // Normal execution

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.

public function manipulateArray(array $anArray) {

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.

心凉 2024-11-23 10:59:02

您可以使用以下命令检查它们传递的是否是字符串:
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.

四叶草在未来唯美盛开 2024-11-23 10:59:02
function sayHello($aName) {
  if (is_string($aName)) {
    //string OK!
  } else {
    echo "sayHello() only takes strings!";
  }
}
function sayHello($aName) {
  if (is_string($aName)) {
    //string OK!
  } else {
    echo "sayHello() only takes strings!";
  }
}
暖阳 2024-11-23 10:59:02

在 PHP 中,您可以使用 is_string 函数检查传递的变量是否为字符串:

 <?php 
 if (is_string($aName)) {
     echo "Yes";
 } else {
     echo "No";
 }
 ?>

希望有所帮助。

In PHP you can check whether the variable that has been passes is a string by using the is_string function:

 <?php 
 if (is_string($aName)) {
     echo "Yes";
 } else {
     echo "No";
 }
 ?>

Hope that helps.

空心空情空意 2024-11-23 10:59:02

或者/另外使用类型转换将变量转换为所需类型

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

十二 2024-11-23 10:59:02

您可以选择检查以确保参数的类型正确。然而,值得考虑的是,如果不是这样,你会怎么做。如果您只是要抛出异常,那么最好假设它是正确的类型,并且当您所做的事情不允许时抛出异常。如果您不打算向已经抛出的异常/错误添加任何更有用的信息,那么首先检查它就没有多大意义。

至于向用户指示您想要什么类型,我通常坚持将其包含在变量名称中:

function sayHello($aNameStr)
function addItems($itemList)
...etc...

加上合理的文档,这意味着用户可以查看该函数并弄清楚他们应该在变量名称中传递什么内容首位。

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:

function sayHello($aNameStr)
function addItems($itemList)
...etc...

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.

2024-11-23 10:59:02

某些脚本语言具有可以帮助您的工具。例如,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文