相关疑难解决方法(0)

限制函数参数以允许特定值

我想限制某人可以在我的函数调用中分配给变量的值。例如说我的功能是

function myFunction ($carType, $isOld , $something)
{
    //do what is required
}
Run Code Online (Sandbox Code Playgroud)

我希望我应该能够限制 $carType, $isOld 仅具有某些值,例如来自给定数组的值:

$carTypeValues = array ("car1", "car2", "car3");
$isOldValues = array (true,false);
Run Code Online (Sandbox Code Playgroud)

我知道我可以将参数变量限制为某种类类型。

我试图找到一种方法来做到这一点,但我找不到任何能完全满足我想要的东西。我知道一旦实际执行函数调用我就可以检查该值,但是我正在寻找一些允许用户最好能够使用选择器的东西,例如:

myFunction (carType::car1, isOld::car2 , $something);
Run Code Online (Sandbox Code Playgroud)

我不确定,这可能是:

myFunction (carType.car1, isOld.car2 , $something);
Run Code Online (Sandbox Code Playgroud)

感谢您阅读本文

php function

5
推荐指数
1
解决办法
3518
查看次数

通过字符串从枚举获取 Case

我寻找一个简单的解决方案来通过字符串获取枚举的情况。有 BackedEnums。例如:

<?php
enum Status: string
{
    case OK = "OK";
    case FAILED = "FAILED";
    ...
}
$status = Status::tryFrom("OK"); // or from("OK");
Run Code Online (Sandbox Code Playgroud)

但我不想为了得到这个结果而写两次同一个单词。有没有一种本地方法可以在没有 BackedEnums 的情况下获取案例?我想要这样的东西:

<?php
enum Status
{
    case OK;
    case FAILED;
    ...
}
$status = Status::get("OK"); //returns Status::OK;
Run Code Online (Sandbox Code Playgroud)

或者我需要为此编写自己的功能吗?例如:

enum Status
{
    case OK;
    case FAILED;    
    public static function get(string $name): null|Status
    {
        $name = strtoupper(trim($name));
        if(empty($name))
            return null;

        foreach(Status::cases() as $status)
        {
            if($status->name == $name)
                return $status;
        }
        return null;
    }
}

Status::get("OK"); // -> Status::OK …
Run Code Online (Sandbox Code Playgroud)

php enums php-8.1

5
推荐指数
2
解决办法
9324
查看次数

PHP按位枚举

我试图根据这个答案做一些像PHP中的按位枚举.但是,当我将所有常量定义为常规ints时,它工作得很好:

final class CountryEnum {
    const CZ = 1;  //Czech Republic
    const DE = 2;  //Germany
    const DK = 4;  //Denmark
    //12 more
    const US = 32768; //USA
}
Run Code Online (Sandbox Code Playgroud)

当我尝试通过位移模式定义值时,它不起作用,即:

final class CountryEnum {
    const CZ = 1;  //Czech Republic
    const DE = 1 << 1;  //Germany
    const DK = 1 << 2;  //Denmark
    //12 more
    const US = 1 << 15; //USA
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行这个时,PHP会说一个合适的说法

解析错误:解析错误,期望','' or在CountryEnum.php中的';''行[带有const DE的行]

所以我可能错过了一些基本的基本内容,但我很茫然.

php enums

4
推荐指数
1
解决办法
1413
查看次数

PHP 中枚举的替代方法

有没有在 PHP 中使用枚举的替代方法?我只是问,因为在 C# 中我有返回 emum 的方法,但在 PHP 中没有类似的存在。以下是虚构方法的一些可能的返回类型:

function login()
{
    // logged in
    // log in failed verify account
    // log in failed account suspended
    // log in failed account banned 
}
Run Code Online (Sandbox Code Playgroud)

在 PHP 中执行此类操作的最佳方法是什么?

php

2
推荐指数
1
解决办法
4594
查看次数

如何在php中为函数定义带有常量变量的参数?

可能的重复:
PHP 和 Enums

我想创建一个具有多个参数的函数,其中一个参数有多个常量变量。例如:

<?php
function print($str, $num){...}
.
.
.
print("omid",one);
print("omid",two);
?>
Run Code Online (Sandbox Code Playgroud)

在这个例子中$num由常量变量组成:“一,二,三”现在,如何实现它?php中有Enum吗?

谢谢你的时间

php

1
推荐指数
1
解决办法
8061
查看次数

使用静态字段作为方法的配置选项

我在Java中注意到了一种模式,我想知道它是否有一个名称,所以我可以查找其他语言的实现.

我已经看到用作方法参数的类的静态final字段.这会将方法的调用者限制为该参数的公共值集.例如:

public class Calendar {

    public static final int JANUARY = 1;
    public static final int FEBRUARY = 1;
    //and so on

    public void setMonth(int month){
       //set month
    }
}

Calendar c = new Calendar();
c.setMonth(Calendar.JANUARY);
Run Code Online (Sandbox Code Playgroud)

这个模式有名字吗?谢谢...

(编写为工作代码的例子,自从我编写Java以来​​已经很久了)

java design-patterns

0
推荐指数
1
解决办法
75
查看次数

标签 统计

php ×5

enums ×2

design-patterns ×1

function ×1

java ×1

php-8.1 ×1