Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a global PHP CONSTANT available inside of a Class file?

Tags:

php

constants

Is a global PHP CONSTANT available inside of a Class file?

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

Then in my class file I try this

public $debug_file = SITE_PATH. 'debug/debug.sql';

This does not seem to work though,

Parse error: parse error, expecting ','' or';'' in C:\webserver\htdocs\somefolder\includes\classes\Database.class.php on line 21

like image 988
JasonDavis Avatar asked Dec 29 '22 05:12

JasonDavis


1 Answers

I second what the others said. Since $debugFile seems an optional dependency, I'd suggest to initialize a sane default on creation of the class and then allow changing it by setter injection when needed, e.g.

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

class Klass
{
    protected $_debugFile;
    public function __construct()
    {
        $this->_debugFile = SITE_PATH. 'debug/debug.sql' // default
    }
    public function setDebugFile($path)
    {
        $this->_debugFile = $path // custom
    }
}

Note that injecting SITE_PATH, instead of hardcoding it, would be even better practice.

like image 71
Gordon Avatar answered Jan 13 '23 15:01

Gordon