Click here to get back home

$stringVar::constant?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
$stringVar::constant? Gordon 06-27-2008
Get Chitika Premium
Posted by Gordon on June 27, 2008, 12:15 pm
Please log in for more thread options
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?

Posted by Jerry Stuckle on June 28, 2008, 9:46 am
Please log in for more thread options
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.
jstucklex@attglobal.net
==================

Posted by Michael Fesser on June 28, 2008, 10:13 am
Please log in for more thread options
.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

Posted by Gordon on June 28, 2008, 12:01 pm
Please log in for more thread options
> .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. =A0I 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. =A0Can anyone help out here?
>
> >Not real nice, but it can be done...
>
> ><?php
>
> >class FirstClass {
> > =A0 =A0 const VAL =3D 'First val';
> >}
>
> >class SecondClass {
> > =A0 =A0 const VAL =3D 'Second val';
> >}
>
> >$cname =3D 'FirstClass';
> >$str =3D $cname . '::VAL;';
> >eval('echo ' . $str . ';');
> >echo "\n";
>
> >$cname =3D 'SecondClass';
> >$str =3D $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...

Posted by Charles Calvert on July 4, 2008, 2:38 pm
Please log in for more thread options
On Fri, 27 Jun 2008 09:15:36 -0700 (PDT), Gordon

>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.php> for
more info.

Similar ThreadsPosted
constant January 20, 2006, 9:17 pm
use C constant in php April 7, 2006, 10:02 am
Constant or Variable? December 18, 2004, 4:07 am
CURLOPT_HTTPPOST - undefined constant September 23, 2004, 5:56 pm
Initialize Class with Constant May 22, 2005, 11:42 am
define a constant array October 9, 2007, 3:07 pm
Constant for maximum integer value? November 15, 2008, 5:11 pm
Best Practice - Constant Page Layouts April 2, 2007, 11:56 am
single quote in string constant December 2, 2007, 2:41 pm
Trying to get constant from array but get literal string instead July 21, 2006, 6:10 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap