473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

$stringVar::constant?

I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.

For example: I have a situation where I have a script that can deal
with objects of one of several classes, but each class needs some
slightly different setup parameters. I'm currently taking care of
this with a switch statement, but i'd like to eliminate it if at all
possible. As it stands, my code is along the lines of:

$thisClass = 'FirstClass'; // Will come from an external source in
real life, validated of course!

switch ($thisClass)
{
case 'FirstClass' :
$val = 'first val';
break;
case 'SecondClass' :
$val = 'second val';
break;
case 'ThirdClass' :
$val = 'third val';
break;
// ...
}

echo ($val);

I'd rather do something along the lines of

class FirstClass
{
const VAL = 'First val';
}
//...

echo ($thisClass::VAL);

I'm guessing that there is some function that returns a class from a
string, but I can't find it. Can anyone help out here?
Jun 27 '08 #1
4 1302
Gordon wrote:
I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.

For example: I have a situation where I have a script that can deal
with objects of one of several classes, but each class needs some
slightly different setup parameters. I'm currently taking care of
this with a switch statement, but i'd like to eliminate it if at all
possible. As it stands, my code is along the lines of:

$thisClass = 'FirstClass'; // Will come from an external source in
real life, validated of course!

switch ($thisClass)
{
case 'FirstClass' :
$val = 'first val';
break;
case 'SecondClass' :
$val = 'second val';
break;
case 'ThirdClass' :
$val = 'third val';
break;
// ...
}

echo ($val);

I'd rather do something along the lines of

class FirstClass
{
const VAL = 'First val';
}
//...

echo ($thisClass::VAL);

I'm guessing that there is some function that returns a class from a
string, but I can't find it. Can anyone help out here?

Not real nice, but it can be done...

<?php

class FirstClass {
const VAL = 'First val';
}

class SecondClass {
const VAL = 'Second val';
}

$cname = 'FirstClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";

$cname = 'SecondClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";

?>

Outputs

First val
Second val

But I don't like it :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 28 '08 #2
..oO(Jerry Stuckle)
>Gordon wrote:
>I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.
[...]

echo ($thisClass::VAL);

I'm guessing that there is some function that returns a class from a
string, but I can't find it. Can anyone help out here?


Not real nice, but it can be done...

<?php

class FirstClass {
const VAL = 'First val';
}

class SecondClass {
const VAL = 'Second val';
}

$cname = 'FirstClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";

$cname = 'SecondClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";

?>

Outputs

First val
Second val

But I don't like it :-)
Neither do I. According to the manual this

echo $thisClass::VAL;

should become possible with PHP 5.3. Until then the Reflection API might
be another option, but I haven't tried it.

Micha
Jun 28 '08 #3
On Jun 28, 3:13*pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Jerry Stuckle)
Gordon wrote:
I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. *I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.
[...]
echo ($thisClass::VAL);
I'm guessing that there is some function that returns a class from a
string, but I can't find it. *Can anyone help out here?
Not real nice, but it can be done...
<?php
class FirstClass {
* * const VAL = 'First val';
}
class SecondClass {
* * const VAL = 'Second val';
}
$cname = 'FirstClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";
$cname = 'SecondClass';
$str = $cname . '::VAL;';
eval('echo ' . $str . ';');
echo "\n";
?>
Outputs
First val
Second val
But I don't like it :-)

Neither do I. According to the manual this

echo $thisClass::VAL;

should become possible with PHP 5.3. Until then the Reflection API might
be another option, but I haven't tried it.

Micha
Thanks. Further investigation turned up the eval() option but I
really don't like using eval(), especially on user-provided data, even
if it has been validated. You never know what you might have missed.
On the other hand reflection might be overkill...
Jun 28 '08 #4
On Fri, 27 Jun 2008 09:15:36 -0700 (PDT), Gordon
<go**********@ntlworld.comwrote in
<de**********************************@c65g2000hsa. googlegroups.com>:
>I'm trying to get a constant from a class, where the constant's name
is known, but the class name isn't. I want to do things this way
because I want classes to be able to define certain aspects for their
setup themselves.

For example: I have a situation where I have a script that can deal
with objects of one of several classes, but each class needs some
slightly different setup parameters. I'm currently taking care of
this with a switch statement, but i'd like to eliminate it if at all
possible. As it stands, my code is along the lines of:

$thisClass = 'FirstClass'; // Will come from an external source in
real life, validated of course!

switch ($thisClass)
{
case 'FirstClass' :
$val = 'first val';
break;
case 'SecondClass' :
$val = 'second val';
break;
case 'ThirdClass' :
$val = 'third val';
break;
// ...
}

echo ($val);

I'd rather do something along the lines of

class FirstClass
{
const VAL = 'First val';
}
//...

echo ($thisClass::VAL);

I'm guessing that there is some function that returns a class from a
string, but I can't find it. Can anyone help out here?
There are a couple of OO options, assuming that you're using php 5.

1. Inheritance. If you can make all of the classes inherit from a
base class, make the constant a member of the base class, then
override it in the derived classes. You can then access the constant
by assigning the instance of the derived class to a variable declared
as the base class.

2. Interface. If #1 isn't feasible, create an interface that contains
the required constant, then make each class implement that interface.
You can then access the instance of the class via a variable declared
as the interface.

See <http://www.php.net/manual/en/language.oop5.constants.phpfor
more info.
Jul 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob Greschke | last post by:
Root.option_add("*?????*font", "Helvetica 12 bold") Want to get rid of the "font =": Widget.add_cascade(label = "File", menu = Fi, font = "Helvetica 12 bold") Does anyone know what ?????...
7
stealwings
by: stealwings | last post by:
Hello everyone; I really need help cause I'm going nowhere trying to figure it out myself, well I have this problem planted: 1) I need to read the content of an txt file (which...
6
by: Time Waster | last post by:
Java property files are dead simple: key1=val1 some.key2=val2 For simplicity on the Java side, I'd like to use these files from C as well (the C program and Java program must cooperate). ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.