Asked  1 Year ago    Answers:  5   Viewed   6 times

I am trying to initialize some values inside a class and save them in constant and access them outside, in different part of my code.

<?php

class Config {

  public static function initialize() {
    define('TEST',"This is a Constant");
  }

}

$config = Config::initialize();
// do something with the constants

Can I access it outside?

 Answers

3

A Class constant uses the const keyword. You don't define them using the define function. Just like this:

class Config {
        const TEST = "This is a constant";
}

// then use it:
var_dump(Config::TEST);

In PHP, you cannot dynamically set the value of a constant, but you can get a similar behaviour with a public static variable. ie.

class Config2 {
    public static $test = null;
    public static function initialize()
    {
        self::$test = "This is not a constant";
    }
}

// Then use like
Config2::initialize();
var_dump(Config2::$test);

The downside is, there is nothing stopping other code from setting the value from outside the class. If you need protection against this, you should use a getter function approach. eg.

class Config3 {
    private static $_test = null;
    public static function initialize()
    {
        self::$_test = "This is not a constant, but can't be changed outside this class";
    }

    public static function getTest()
    {
        return self::$_test;
    }
}

// Then use like
Config3::initialize();
var_dump(Config3::getTest());
Thursday, April 1, 2021
 
1

You don't. Constants are constant. You can't store anything in them.

You can use a static property though.

class My_Class {
  public static $DB_TABLE;
}
My_Class::$DB_TABLE = TABLE_PREFIX . 'class_table';

You can't do it within the declaration, so you might prefer a static method instead.

class My_Class {
  public static function dbTable() {
    return TABLE_PREFIX . 'class_table';
  }
}
Thursday, April 1, 2021
 
KHM
 
KHM
4

A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase.

The name of a constant follows the same rules as any label in PHP. A valid constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thusly: [a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*

From: Constants (PHP mamual)

Thursday, April 1, 2021
 
Sagar
 
4

Define statements are in general only commented with a descriptive text, so that's basically how you comment it.

To read more about the DocBlock template tag, /**#@+, check out the manual page.

Saturday, May 29, 2021
 
viper
 
4

re your comment, sounds like you need a root web context object that you reference the other objects from:

$ctx = WebContext::get();
$ctx->session->get('x');
$ctx->input->get('y');
$ctx->identity->valid;

etc... this is how most web frameworks do it.

$session would need to be defined, then referenced in the included file:

// If a global variable:
global $session;
$session->get('x');

// If a member of Core:
$this->session->get('x');

yes you can do that, probably you'll want require instead of require_once, and the paths would need to be based on the current working directory or an absolute path

try adding error_reporting(E_ALL) to see if any notices are happening...

Saturday, May 29, 2021
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :