class SimpleClass
{
const $lookupA = 1;
const $lookupB = 2;
const $lookupC = 3;
public function displayVar(llookup*) {
return $lookup*;
}
}
class SimpleClass
{
const $lookupA = 1;
const $lookupB = 2;
const $lookupC = 3;
public function displayVar(llookup*) {
return $lookup*;
}
}
What are you expecting this to do? $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
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;
}
}
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.
public $lookup = '';//define property (variable)
<?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);
public function test($var1) {
if (!is_array($var1,array($TypehintsBool, $TypehintsInt, $TypehintsFloat, $TypehintsString, $TypehintsResource))) {
die("var1 parameter is not valid");
}
}
if (!is_array($var1,array($TypehintsBool, $TypehintsInt, $TypehintsFloat, $TypehintsString, $TypehintsResource)))
Did you mean in_array() - is_array only takes one parameter and its use here makes no sense.$tt->test(test66::$NoSuchValue);
<?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);
?>
<?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";
}
<?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
Outside the class
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