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?
PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Peos John

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
Julian Hansen

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.
Peos John

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Julian Hansen

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.
Peos John

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

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Julian Hansen

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
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tonelm54

ASKER
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

Julian Hansen

@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()
Your help has saved me hundreds of hours of internet surfing.
fblack61
Julian Hansen

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

Ray Paseur

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
Julian Hansen

@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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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.