4

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

aimme
  • 6,385
  • 7
  • 48
  • 65
user2636664
  • 401
  • 1
  • 5
  • 14
  • Someone picked PHP book instead of C from the shelf :-D – zerkms Jul 11 '14 at 01:33
  • yeah..I am actually more of a Java developer and relatively new to PHP, loose language has some disadvantages as well :) – user2636664 Jul 11 '14 at 01:35
  • Please see this comprehensive answer: https://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) {...`. – Duncanmoo May 19 '20 at 09:39

3 Answers3

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.

t j
  • 7,026
  • 12
  • 46
  • 66
  • 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 – user2636664 Jul 11 '14 at 01:44
  • 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! – t j Jul 11 '14 at 01:48
  • 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 https://martinfowler.com/bliki/ValueObject.html Here we would need a CarType class, that allowes certain car types only in its constructor – Oliver Kurmis Jun 14 '21 at 08:32
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);
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • 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. – user2636664 Jul 11 '14 at 01:39
  • 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... – scrowler Jul 11 '14 at 01:44
  • this is a standalone function, not a part of a class. Thanks – user2636664 Jul 11 '14 at 01:48
  • Ok. Can you provide a little more information on the reason and requirements for such a structure and I will try and help further – scrowler Jul 11 '14 at 01:49
  • 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. – user2636664 Jul 11 '14 at 01:57
  • I'd just put in a variable type filter like you've said you can do already, write some decent documentation and tell other developers not to be lazy. Good luck – scrowler Jul 11 '14 at 01:59
0

PHP language has no Enum type (proposition only).

But you could obtain splEnum by installing special extension.

PHP and enumerations question

Community
  • 1
  • 1
sectus
  • 15,605
  • 5
  • 55
  • 97