Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

PHP function parameters defined

When I execute some SQL functions I get a list of possible variables, but cant find any documentation on how to complete something similar.

For example:-
class SimpleClass
{
    const $lookupA = 1;
    const $lookupB = 2;
    const $lookupC = 3;

    public function displayVar(llookup*) {
        return $lookup*;
    }
}

Open in new window


So, if I typed myNewClassInstance->displayVar( into my IDE my choices would be lookupA,lookupB or lookupC and would return a value 1-3. This is just a simple class to describe what Im trying to do, its not the best implementation.

Any ideas?
Avatar of Peos John
Peos John
Flag of Malaysia image

class SimpleClass
{
    public $lookup = '';//define property (variable)

    public function displayVar($lookup)
    {
        return $lookup;
    }
}

Open in new window


Outside the class

$obj = new SimpleClass();
$lookup_input = 1;//1,2 or 3
$return_value = $obj->displayVar($lookup_input); // store output in a variable 

echo return_value;

Open in new window




http://php.net/manual/en/language.oop5.php

Constants should be defined like below

http://php.net/manual/en/language.oop5.constants.php

Here is the good explanation about visibility

http://php.net/manual/en/language.oop5.visibility.php
I don't understand what you are doing here
class SimpleClass
{
    const $lookupA = 1;
    const $lookupB = 2;
    const $lookupC = 3;

    public function displayVar(llookup*) {
        return $lookup*;
    }
}

Open in new window

What are you expecting this to do?
You have an illegal parameter variable (llookup*)
You have an illegal return value - which appears to be dynamically targeting a member property which would have to be accessed through $this if you wanted the value.

Nothing about this code looks remotely valid and does not appear to make sense. I might be missing something - can you fill in the gaps.
Sorry!  Just noticed from my previous comment, the return value should have $ symbol. Below is the corrected code.

$obj = new SimpleClass();
$lookup_input = 1;//1,2 or 3
$return_value = $obj->displayVar($lookup_input); // store output in a variable 

echo $return_value;//code updated

Open in new window

class SimpleClass
{
    // Not a const
    public $lookup = '';//define property (variable)

    public function displayVar($lookup)
    {
        // this just returns the paramter value passed in - it does not touch the 
        // the member property. You need to access that with $this->$lookup;
        return $lookup;
    }
}

Open in new window

I think this is a misdirection - the author needs to explain what he is trying to do. He appears to be trying to display a class constant by passing an index to the display function - which is not going to work. Before suggesting a solution I recommend we find out what the use case is.
Agreed! But I have mentioned $lookup is a variable in comment, so it is not a constant.  

 public $lookup = '';//define property (variable)

Open in new window

Avatar of tonelm54
tonelm54

ASKER

Sorry, I think its gone off topic.

Using a little more googling Ive managed to do this:-
<?php
class test66 {
    public static $TypehintsBool = "is_bool";
    public static $TypehintsInt = "is_int";
    public static $TypehintsFloat = "is_float";
    public static $TypehintsString = "is_string";
    public static $TypehintsResource = "is_resource";

    public function test($var1) {
        
    }
}

$tt = new test66();
$tt->test(test66::$TypehintsInt);

Open in new window


My problem is that the test function can accept anything as var1, I would like to limit it to only to accept a range a constant values instead, eg only ($TypehintsBool, $TypehintsInt, $TypehintsFloat, $TypehintsString, $TypehintsResource).

I thought I could test to see if $var1 was allowed by:-
    public function test($var1) {
        if (!is_array($var1,array($TypehintsBool, $TypehintsInt, $TypehintsFloat, $TypehintsString, $TypehintsResource))) {
            die("var1 parameter is not valid");
        }        
    }

Open in new window


But it didnt work, but with a bit more googling Im sure I could get it to work, however it seems the wrong way to go, surely there is something I can type into the function parameters arguments to limit its selection?

Thank you
if (!is_array($var1,array($TypehintsBool, $TypehintsInt, $TypehintsFloat, $TypehintsString, $TypehintsResource))) 

Open in new window

Did you mean in_array() - is_array only takes one parameter and its use here makes no sense.

Checking for existence in the function is redundant - if you try and call the function with a non-existing static value it is going to error on you.

This is going to generate an error before you get to the test() function
$tt->test(test66::$NoSuchValue);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just for reference and anyone else coming across this thread, this works really nicely:-
<?php
class test66 {
    const TypehintsBool = "is_bool";
    const TypehintsInt = "is_int";
    const TypehintsFloat = "is_float";
    const TypehintsString = "is_string";
    const TypehintsResource = "is_resource";
    public function test($var1) {
        if (!in_array($var1,array(test66::TypehintsBool, test66::TypehintsInt))) { die("\$var1 parameter is not valid"); } 
        echo "Will run this";
    }
}

$tt = new test66();
$tt->test(test66::TypehintsBool);
?>

Open in new window

@tonelm54,

If you are going to create an array each time to test for existence - why not define the variables as an array to begin with
<?php
class test66 {
	public static $values = array (
		'TypehintsBool' => "is_bool",
		'TypehintsInt' => "is_int",
		'TypehintsFloat' => "is_float",
		'TypehintsString' => "is_string",
		'TypehintsResource' => "is_resource"
	);

	public function test($var1) {
		if (!in_array($var1, test66::$values)) {
			return false;
		}
		return true;
	}
}

$tt = new test66();
if ($tt->test(test66::$values['TypehintsBool'])) {
	echo "Does exist";
}
else {
	echo "Does not Exist";
}
if ($tt->test('abc')) {
	echo "Does exist";
}
else {
	echo "Does not Exist";
}

Open in new window


Also, consider using return values from your class functions rather than die()
Or even this
<?php
class test66 {
	public static $values = array (
		'TypehintsBool' => "is_bool",
		'TypehintsInt' => "is_int",
		'TypehintsFloat' => "is_float",
		'TypehintsString' => "is_string",
		'TypehintsResource' => "is_resource"
	);

	public function test($var1) {
		if (!isset(test66::$values[$var1])) {
			return false;
		}
		return true;
	}
}

$tt = new test66();
if ($tt->test('TypehintsBool')) {
	echo "Does exist";
}
else {
	echo "Does not Exist";
}

Open in new window

Saying "in PHP a function can take anything as a parameter" oversimplifies an important issue, one we have been on working with PHP for a long time.  It's variously called "type hinting" or "type declaration."  It's a philosophical conundrum in a loosely-typed language, but the compromise PHP has created works about as well as you can expect if you want backward compatibility.

Here are the most relevant man page links.
http://php.net/manual/en/language.oop5.typehinting.php
http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

This concept is explored a little more in the article here, where we use a RequestArguments class:
https://www.experts-exchange.com/articles/18409/Using-Named-Parameters-in-PHP-Function-Calls.html
@Ray,

Correct me if I am wrong but it is only in PHP7 that type hinting for scalar types is supported - prior to PHP7 wasn't it the case that only objects and arrays were supported for type hinting?

Not an area I am overly familiar with so will take your lead.
Julian: Yes, and PHP 7+ is the only actively supported branch.  PHP < 7 will get security fixes for a while longer, but the new features (including expanded type hints) will only be in PHP 7+.  So, it's time to upgrade!  That's part of the reason I wrote about named parameters in PHP function calls.  It's also a pain to have to remember the order and number of arguments, even if there is type declaration.