避免长类常量作为参数 - PHP

时间:2022-06-07 08:38:29

example:

class Vendor_ClassName_Helper {
    CONST FIRST_OPTION  = 1;
    CONST SECOND_OPTION = 2;

    public function __construct($option, $otherArgument) {

    }
}

client code:

$obj = new Vendor_ClassName_Helper(Vendor_ClassName_Helper::FIRST_OPTION, $var);

Any good ways to avoid the long lines (and this is a rather short example)? Maybe other ways to implement the same?

避免长线的任何好方法(这是一个相当简短的例子)?也许其他方法实现相同的?

5 个解决方案

#1


I think clarity is better than short code. You can try to think of different words of expressing the same or different form. For your example, it doesn't seem very bad as Omega pointed out, and his method of splitting declaration on multiple lines is good as well.

我认为清晰度比短码更好。您可以尝试考虑表达相同或不同形式的不同词语。对于你的例子,正如Omega指出的那样,它看起来并不是很糟糕,而且他在多行上分割声明的方法也很好。

Here's another trick: Depending on what your option constants do, you may want to employ a factory method instead of the new-keyword.

这是另一个技巧:根据您的选项常量所做的,您可能希望使用工厂方法而不是new-keyword。

For example,

class Example {
    private function __construct() { }

    public static method createA(..) { 
        //configure for mode A
        $cls = new self; 
        return $cls;
    }

    public static method createB(..) { 
        //configure for mode B
        $cls = new self; 
        return $cls;
    }
}

$x = Example::createA();

#2


I avoid long lines and improve readability in most languages by breaking up the parameters into their own kind of block...

我通过将参数分解为他们自己的块来避免长线并提高大多数语言的可读性......

$obj = new Vendor_ClassName_Helper(
    Vendor_ClassName_Helper::FIRST_OPTION, 
    $var
);

But two options doesn't always warrant it in my opinion. Static constants unfortunately can't really be changed and you do of course want them to remain descriptive.

但在我看来,两种选择并不总能保证。遗憾的是,静态常量无法真正改变,你当然希望它们保持描述性。

What you have here isn't so bad :)

你在这里有什么不是很糟糕:)

#3


If you're passing a constant to the constructor, it would suggest that you should create subclasses instead:

如果您将常量传递给构造函数,则建议您应该创建子类:

class Vendor_ClassName_Helper {
  public function __construct($otherArgument) {
  }
}

class Vendor_ClassName_Helper_First extends Vendor_ClassName_Helper {
}

class Vendor_ClassName_Helper_Second extends Vendor_ClassName_Helper {
}

#4


without using shorter name for class or constant's names (and making your code impossible to understand, which is something you definitly don't want), no, I don't think there is a way -- at least, not in PHP < 5.3

没有使用较短的名称为类或常量的名称(并使你的代码无法理解,这是你绝对不想要的东西),不,我不认为有办法 - 至少,不是在PHP <5.3

PHP 5.3 adds namespaces to PHP ; with those, you might be able to come to something shorter / better ; but it means using PHP 5.3, which is not proposed by many hosting companies (5.3.0 was release at the end of june this year, so it might be a while before it's available averywhere...)

PHP 5.3为PHP添加了名称空间;有了这些,你可能会变得更短/更好;但它意味着使用PHP 5.3,这是许多托管公司没有提出的(5.3.0是在今年6月底发布的,所以它可能需要一段时间才可以在其他地方使用......)

For more informations about namespaces in PHP (and to cite only a couple of links) :

有关PHP中名称空间的更多信息(并且仅引用几个链接):

#5


I think there isn't a better way (there isn't a dynamic way):

我认为没有更好的方法(没有动态的方式):

class LongClassName
{
    const B = 3;
}

class LCN
{
    const B = LongClassName::B;
}

echo LCN::B;

#1


I think clarity is better than short code. You can try to think of different words of expressing the same or different form. For your example, it doesn't seem very bad as Omega pointed out, and his method of splitting declaration on multiple lines is good as well.

我认为清晰度比短码更好。您可以尝试考虑表达相同或不同形式的不同词语。对于你的例子,正如Omega指出的那样,它看起来并不是很糟糕,而且他在多行上分割声明的方法也很好。

Here's another trick: Depending on what your option constants do, you may want to employ a factory method instead of the new-keyword.

这是另一个技巧:根据您的选项常量所做的,您可能希望使用工厂方法而不是new-keyword。

For example,

class Example {
    private function __construct() { }

    public static method createA(..) { 
        //configure for mode A
        $cls = new self; 
        return $cls;
    }

    public static method createB(..) { 
        //configure for mode B
        $cls = new self; 
        return $cls;
    }
}

$x = Example::createA();

#2


I avoid long lines and improve readability in most languages by breaking up the parameters into their own kind of block...

我通过将参数分解为他们自己的块来避免长线并提高大多数语言的可读性......

$obj = new Vendor_ClassName_Helper(
    Vendor_ClassName_Helper::FIRST_OPTION, 
    $var
);

But two options doesn't always warrant it in my opinion. Static constants unfortunately can't really be changed and you do of course want them to remain descriptive.

但在我看来,两种选择并不总能保证。遗憾的是,静态常量无法真正改变,你当然希望它们保持描述性。

What you have here isn't so bad :)

你在这里有什么不是很糟糕:)

#3


If you're passing a constant to the constructor, it would suggest that you should create subclasses instead:

如果您将常量传递给构造函数,则建议您应该创建子类:

class Vendor_ClassName_Helper {
  public function __construct($otherArgument) {
  }
}

class Vendor_ClassName_Helper_First extends Vendor_ClassName_Helper {
}

class Vendor_ClassName_Helper_Second extends Vendor_ClassName_Helper {
}

#4


without using shorter name for class or constant's names (and making your code impossible to understand, which is something you definitly don't want), no, I don't think there is a way -- at least, not in PHP < 5.3

没有使用较短的名称为类或常量的名称(并使你的代码无法理解,这是你绝对不想要的东西),不,我不认为有办法 - 至少,不是在PHP <5.3

PHP 5.3 adds namespaces to PHP ; with those, you might be able to come to something shorter / better ; but it means using PHP 5.3, which is not proposed by many hosting companies (5.3.0 was release at the end of june this year, so it might be a while before it's available averywhere...)

PHP 5.3为PHP添加了名称空间;有了这些,你可能会变得更短/更好;但它意味着使用PHP 5.3,这是许多托管公司没有提出的(5.3.0是在今年6月底发布的,所以它可能需要一段时间才可以在其他地方使用......)

For more informations about namespaces in PHP (and to cite only a couple of links) :

有关PHP中名称空间的更多信息(并且仅引用几个链接):

#5


I think there isn't a better way (there isn't a dynamic way):

我认为没有更好的方法(没有动态的方式):

class LongClassName
{
    const B = 3;
}

class LCN
{
    const B = LongClassName::B;
}

echo LCN::B;