Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String alternative of constants? #4

Open
jehoshua02 opened this issue Apr 2, 2015 · 2 comments
Open

String alternative of constants? #4

jehoshua02 opened this issue Apr 2, 2015 · 2 comments

Comments

@jehoshua02
Copy link
Member

<?php

$distance = new \Reusables\Distance(12, 'inches');
$distance = new \Reusables\Distance(1, 'inch');
$distance = new \Reusables\Distance(12, Distance::INCHES);
@jehoshua02 jehoshua02 changed the title Strings instead of constants? String alternative of constants? Apr 2, 2015
@jehoshua02
Copy link
Member Author

I think this would just require a mapping from strings to constants. Constants will all be in plural all caps.

<?php

class Distance {
  protected static $units = array(
    'inches' => self::INCHES,
    'inch' => self::INCHES
  );
}

This probably involves some strtolower before looking up the constant.

@jehoshua02
Copy link
Member Author

Or ... the mapping keys can be uppercase and we do strtoupper. That way we can even use constants for the keys and have a single mapping array for singular/plural lookups too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant