koen
koen

Reputation: 13757

avoiding long class constants as parameters - PHP

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?

Upvotes: 0

Views: 385

Answers (5)

JeromeJ
JeromeJ

Reputation:

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;

Upvotes: 0

troelskn
troelskn

Reputation: 117567

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 {
}

Upvotes: 2

Pascal MARTIN
Pascal MARTIN

Reputation: 401142

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 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...)

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

Upvotes: 1

Jani Hartikainen
Jani Hartikainen

Reputation: 43253

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.

Here's another trick: Depending on what your option constants do, you may want to employ a factory method instead of the 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();

Upvotes: 4

Alexander Trauzzi
Alexander Trauzzi

Reputation: 7395

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 :)

Upvotes: 3

Related Questions