5

I want to restrict the values that someone can assign to a variable in my function call. For e.g. say my function is

function myFunction ($carType, $isOld , $something)
{
    //do what is required
}

I want that I should be able to restrict $carType, $isOld have only certain values, say from given arrays:

$carTypeValues = array ("car1", "car2", "car3");
$isOldValues = array (true,false);

I am aware that I can restrict the parameter variable to be of a certain class type.

I have tried to find a way to do this, but I am not able to find anything that addresses exactly what I want. I know I can check the value once I actually execute the function call, however I am looking for something that will allow the user to preferably be able to use selectors like:

myFunction (carType::car1, isOld::car2 , $something);

I am not sure, this maybe:

myFunction (carType.car1, isOld.car2 , $something);

Thanks for reading this

3
  • Someone picked PHP book instead of C from the shelf :-D Commented Jul 11, 2014 at 1:33
  • yeah..I am actually more of a Java developer and relatively new to PHP, loose language has some disadvantages as well :) Commented Jul 11, 2014 at 1:35
  • Please see this comprehensive answer: stackoverflow.com/a/27721900/352040. Personally I like the class approach, and then limit the function argument to an object of that type like function foo(SomeClass $data) {.... Commented May 19, 2020 at 9:39

3 Answers 3

2

A number of ways you could do this. The quick and dirty way is to manually check values in the function and process them accordingly:

function myFunction($carType, $isOld, $something)
{
    if (in_array($carType, array('car1', 'car2', 'car3'))) {
        echo 'Car type is valid';
    } 
    if (is_bool($isOld)) {
        echo 'Car age is valid';
    } 
}

The other way is to typehint the function definition with objects that validate the input. PHP doesn't support type hinting for all types so objects may be the way to go but this may be overkill for this situation.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I know I can do this as I said in my question, however I am looking for something which can have hints or something to tell the user who calls the function that it ain't correct. I am starting to think it may not be worth bothering...there are some really great users on stackoverflow (like yourself) who many a times amaze me and thus I thought of asking if there is something simple that I am not thinking about. Thanks
I think that you're trying to do something that PHP simply isn't capable of doing. It doesn't support enumerated type hinting or anything more complex than basic object and array hinting. If you need this level of control you'd be better off with another language I'm afraid. Good luck with it though!
You can do something like this very well with PHP. This is a good use case for the Value object pattern, habe a look at martinfowler.com/bliki/ValueObject.html Here we would need a CarType class, that allowes certain car types only in its constructor
1

Are you looking for a simple kind of validation for what variable is passed into your function, something like this?

function myFunction ($carType = null, $isOld = null, $something = null)
{
    $carTypeValues = array ("car1", "car2", "car3");
    $isOldValues = array (true,false);

    $strict = true; // strict comparison here? e.g. null == false if $strict is false

    if(
        !$carType || !$isOld                            // variables aren't empty?
        || !in_array($carType, $carTypeValues, $strict) // passed carType is ok?
        || !in_array($isOld, $isOldValues, $strict)     // passed isOld is ok?
    ) {
        // error, incorrect variable passed
        return false;
    }

    //do what is required
    return true;
}

This will allow you to check that A. your variables passed exist, and B. that they exist within an array of acceptable options that you define.


I know I can check the value once I actually execute the function call, however I am looking for something that will allow the user to preferably be able to use selectors

You can't specify selectors like in your example, but you could pass an integer in to represent an array key, thus avoiding the need to duplicate the variable's value:

function myFunction($carType = null, $isOld = null, $something = null) { 
    $carTypeValues = array ("car1", "car2", "car3");
    $isOldValues = array (true,false);  

    if(
        !$carType || $isOld
        || !array_key_exists($carType, $carTypeValues)
        || !array_key_exists($isOld, $isOldValues)
    ) {
        return false;
    }

    // handle OK
    return array(
        'car_type' => $carTypeValues[$carType],
        'is_old'   => $isOldValues[$isOld]
    );
}

myFunction(1, 0, 'foobar'); // array('car_type' => 'car2', 'is_old' => true);

6 Comments

Yes, I know I can do this as I said in my question, however I am looking for something which can have hints or something to tell the user. I am not saying this is necessarily possible.
If this is part of a class, you can set your possible values to a public property and have a method that returns them so you can present possible options to the user...
this is a standalone function, not a part of a class. Thanks
Ok. Can you provide a little more information on the reason and requirements for such a structure and I will try and help further
oh, there is no concrete requirements to have this, it would be a good to have so that other developers call the function properly in the first place..but as I said, it's not worth anymore time being spent on it. Thanks for your help mate.
|
0

PHP language has no Enum type (proposition only).

But you could obtain splEnum by installing special extension.

PHP and enumerations question

1 Comment

I know, I looked at splEnum, unfortunately I can't get that installed on our server. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.