PHP OO Static Var Question...

I have a class like this:

class foo {
//Static Vars
public static dogName = 'Elmo';

//But I want to do this:
if($GLOBALS['showName']) {
     public static dogName = 'Elmo';
} else {
     public static dogName = '';
}

It doesn't seem to be possible though. I know I can do it in the contructor, but then I'd have to instantiate the class to get the right value. Is there anyway to achieve something like this?
interclubsAsked:
Who is Participating?
 
interclubsAuthor Commented:
Apparently, you can do this, which works:
class foo {
//Static Vars
public static dogName = 'Elmo';

} //End Class

//Now Configure Static Vars
if($GLOBALS['showName']) {
     foo::dogName = 'Elmo';
} else {
     foo::dogName = '';
}
0
 
4e4enCommented:
class foo {
//Static Vars
public static dogName = null;
function __construct(){
if($GLOBALS['showName']) {
     self::dogName = 'Elmo';
}
}
}

Open in new window

0
 
Beverley PortlockCommented:
Why not just instantiate the object?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
have to instantiate the class to get the right value - sure, that makes sense to me.

I agree with bportlock.  Put the code you want to run in the constructor and instantiate the object.  

But maybe there is some deeper background to this question that we are not seeing, since trying to get conditional logic to work without running the conditional logic is an "unusual" computer science idea.  If you want to tell us a little more about what you're trying to achieve, in plain, non-technical language, we may be able to suggest a more workable design pattern.
0
 
Mohamed AbowardaSoftware EngineerCommented:
You can't use if statements directly in the class, you must use it inside a method or directly in the php script.

What you will have to do is declare the variable normally and set its value using __construct().

Classes which have a constructor method call __construct() method on each newly-created object.
http://php.net/manual/en/language.oop5.decon.php
0
 
Slick812Commented:
Not sure I understand your "Idea" or reasoning for having a static string variable in your class, as it is independent of methods in that class, and can be accessed without creating an instance of that class. It may be un workable to use the class constructor, as you have indicated, but since your $dogName is there without having a call  $foo1 = new foo();  - - - you can just call the if ($GLOBALS) statement at any point in your PHP script where you need an "Update" for the $dogName in foo, like

echo 'time to check the showName and update dogname<br />';
if($GLOBALS['showName']) {
     $foo::$dogName = 'Elmo';
}
else
 $foo::$dogName = 'not GLOBAL';

echo 'New dogname - '.$foo::$dogName;
0
 
Ray PaseurCommented:
Or maybe just put the dogName element in $GLOBALS and use it directly from there... No extra credit for copying a variable from one location to another.
0
 
Mohamed AbowardaSoftware EngineerCommented:
@interclubs: You question was obviously asking to change the variable when declaring it.

We have provided solution for your question and you simply added another solution and now wanting to accept only your own solution and ignoring all our solutions which will do exactly the same.

Since we helped with possible solutions, you shouldn't accept your own solution only and close the question.
0
 
interclubsAuthor Commented:
My questionally specifically said how to change the variables WITHOUT using the construct, allowing you to directly reference the static variables on other pages w/o having to instantiate the class or do anything else.

So now anywhere you can do <?= foo::$dogName ?> and it will have the write answer. For what it's worth, I didn't even know it was possible to add code after the end of the class that would be executed when using an auto loader. Works nicely though.
0
 
Mohamed AbowardaSoftware EngineerCommented:
Sorry, I didn't noticed "without using construct" part, though I think that http:35453804 and http:35454133 helped.

You can close this question as you like and if any of the experts think the question should be closed differently they can post objection.
0
 
South ModModeratorCommented:
All,
 
Following an 'Objection' by Medo3337 (at http://www.experts-exchange.com/Q_26973908.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.