0

I was just researching this for the past hour to try and figure out the best way to do this.

I'm working on a PHP CLI application that uses the following function to find the command line width.

function getColLen() {
    $getcolLen = shell_exec('tput cols');
    $colLen = (int) $getcolLen;
    return $colLen; 
}

Now, I have around 8 classes I'm working with right now, and this function I am using quite a bit -- in most classes and in a lot of methods within the classes.

Previously it was just included in a composer vendor autoload functions.php file and I was initializing it in each function, like

public function Tagger()
    {
$colwidth = getColLen();

. . . . . 

etc.

I researched many different options for using a dynamic global variable across PHP classes, out of which there are many and some get quite complicated.

For fun I decided to try and this to a CONSTANT in my functions.php file

define('CLIWIDTH', getColLen());

I did not think this would work, as all the information I read -- and all the examples I saw -- were about defining static variables such as a string or directory.

But to my surprise this works perfectly fine. A simple echo CLIWIDTH; anywhere brings in the command line width (tested with success for different outputs when launching the console at different sizes).

Is this not widely recognized as a proper way to create a dynamic variable across all files? Perhaps I wasn't searching the correct terms but I have never heard of someone using a function inside a defined constant such as this.

Is this proper code? Is there a reason why this isn't more recommended when discussing constants, globals, so forth?

1
  • There's no limitation on what the second argument to define() can be. Commented May 2, 2019 at 3:28

2 Answers 2

1

There are no restrictions on what the value argument to define() can be, and the constant will be defined to have whatever that expression is. While it's mostly used just to give names to literals, like database configuration settings, it doesn't have to be. You can put a dynamic expression there, e.g.

define("DB_USER", $hostname == "dev" ? "devuser" : "produser");
Sign up to request clarification or add additional context in comments.

2 Comments

Very cool, I had no idea when learning about defining constants from other sources. Wasn't sure if it just wasn't preferred or what! Honestly it seems like a great option to global scope something dynamically (..if the dynamics are the same for each particular instance.)
That's exactly what define is used for, to create names that are globally scoped. Whether the value is a simple literal or calculated is irrelevant. The difference from ordinary variables is that you can't change them once you define them.
0

This is valid PHP, but it is NOT the same as calling the function each time (unless the function always returns the same result). Basically the function gets called when the define is encountered, and that return value is assigned to the constant. Consider:

function getv() {
    static $v = 4;
    return $v++;
}

define('V', getv());
echo V . PHP_EOL;
echo getv() . PHP_EOL;
echo V . PHP_EOL;

Output:

4
5
4

Note that the constant V always outputs 4, even after the return value of getv changes to 5.

Demo on 3v4l.org

2 Comments

Sorry about the constant editing, was coming up with a better example...
Makes sense, thank you for for the info and example! I like your edit as well, when messing on your Demo link I did the exact same thing to echo V twice to understand then noticed you made the edit exactly as such

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.