Hi .. this is my first post, and I hope I'd be of help to members of this forum.

I noticed the following and I'm not sure if it is a "feature" or bug of my own. I hope I could have your insight into this.

I don't use any constants in my code. I keep all constants in a sparate constants.inc which I keep at the root directory of the php project I'm working on. I also usually prefix my constants lest they might be defined by PHP or somehow get in conflict with other system/environment defined constants.

I know that once constants are defined they are available globally to all other code segments in the application, or so the manual says, however I noticed the following, and I'm not sure if this is correct.

file1.php

<?php

// some session handling and access privs goes here

include ("config.php"); // main app config file -- $gprfx is holding my "global prefix" for constants

include ("constants.inc"); // constants definition file

include (constant($gprfx."DBPATH").$database.$extension); // this loads my db class abstraction file where $database = "mysql"

// and $extension=".php" -- the expression evaluates to db/mysql.php in this example

$dbh = new DB($hostname, $dbusername, $dbpassword); // the DB class is dfined in mysql.php

?>

Now all constants are defined and accessible in the global space of "file1.php" I assume that they should be available to code included in line #4 that evaluates to include ("db/mysql.php");

However, it seems this assumption is invalid, because the constructor call in $dbh=new DB() results in the following warning

Warning: constant(): Couldn't find constant DBNAME in /doc_root/db/mysql.php on line 163

This line in mysql.php looks like this

file: myql.php

<?php
class DB {

// some code goes here

var $Dbname; // should default to a constant defined in constants.inc if not otherwise specified in the DB() constructor call

// ---------------------------------------
// DB constructor
// ---------------------------------------

   function DB($h='localhost', $u='root', $p='', $dbn='', $t='+00:00') {

   // some code goes here

  ($dbn != "") ? $this->Dbname = $dbn : $this->Dbname = constant($gprfx."DBNAME");

   // some more code goes here

   } // End Constructor

}// End class definition
?>

And now when calling DB() from "file1.php" without specifying $dbn, the condition ($dbn !="") evaluates to false and $this->Dbname = constant($gprfx."DBNAME") returns the warning.

Warning: constant(): Couldn't find constant DBNAME in /doc_root/db/mysql.php on line 163

which means the $gprfx."DBNAME" is not a defined constant within the scope of "mysql.php"

On the other hand placing the following debugging statement outside of the class definition:

echo "test from within mysql.php: ".constant($gprfx."DBNAME")."<br>\n";

returns the correct value of the constant on the screen. However the same statement issues the same warning above when placed within any member function of the class including the constructor DB(), of course.

I was just wondering if constants definitions make them like a true superglobals or what?

Any thoughts on this? Much obliged.
Thanks

    Your problem is in the use of $gprfx. In your class it's not known, or has a value assigned (within the DB constructor/function).

    I assume that you're using $gprfx to add some prefix to whatever the value of the constant is. So, why not make $grpfx into a constant as well?

    Constants are global. But you're using a variable along with the constant!

    🙂

      Thank you very much, toplay, I'll take this advise and do some tests and get back to you.
      Thanks again

      ps. you're signature is "awesome" -- both the links and the "keep coding ..." just about what we do everyday, right?

        Write a Reply...