tags:

views:

145

answers:

5

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?

+3  A: 

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

Omega
A: 
+3  A: 

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();
Jani Hartikainen
You mean something like the builder pattern?http://en.wikipedia.org/wiki/Builder_pattern
koen
No, just a factory method instead of a constructor. Sometimes you may be able to name the method so, that even if it communicates that by calling this you get the object configured in that way, it's still shorter than using some constant.
Jani Hartikainen
+1  A: 

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

Pascal MARTIN
+2  A: 

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 {
}
troelskn
How would this approach fit in a class like Zend_Auth_Result?
koen
`class Zend_Auth_Result_Success extends Zend_Auth_Result {}` etc. I presume.
troelskn
That's a good answer. But I was thinking (and haven't written that) that it's rather awkward for client code to have a class Zend_Auth_Result_Success, then asking case per case if eg if $result->getCode() === Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND. The long constant names are gone for the __construct(), but now you have to refer to the constants of another class than the one you got (different if it would be interface constants).
koen
That suggests another problem with your code. You should replace conditional with polymorphism. See: http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
troelskn
It's the client code that has the conditional, like in the ZF manual:http://framework.zend.com/manual/en/zend.auth.html#zend.auth.introduction.results
koen
Well, if you use a third party library/framework then you are of course bound by whatever design decisions they made.
troelskn
Would it improve your proposed solution if the Vendor_ClassName_Helper class was abstract? If not the class would have to check the values as allowed ones (as is the case in my example). With an abstract class this wouldn't be necessary.
koen
I don't understand what you mean by that last comment. It's a bit hard to discuss concrete solutions to abstract problems.
troelskn