0

I've used many php framework , there always be a place (ex: constants.php) to put developer's self defined constants, but I can't find that in Joomla. Could anyone tell me where to put these self-defined constants so that we can use them anywhere in the project, thanks:)

  • 1
    What globally scoped php variables are you intending to declare? And why do you think you need globally scoped variables? Some extensions have constants declared inside of them (for instance: define('_AKEEBA_RESTORATION', 1); defined('DS') or define('DS', DIRECTORY_SEPARATOR);) Notice the importance of checking if a constant is already defined. – mickmackusa Dec 10 '19 at 11:56
  • 1
    The answer at joomla.stackexchange.com/a/14641 and the comments may help. – Neil Robertson Dec 10 '19 at 23:10
  • I just need to use these constants in a project which might utilize more than two components at the same time and just for convenience, I need to access these constants in both components for consistence purpose. – Ryan Ye Dec 11 '19 at 7:43
3

Joomla's front-end entry point is index.php

In line 29-32 Joomla checks for a file in the webroot called defines.php

if (file_exists(__DIR__ . '/defines.php'))
{
    include_once __DIR__ . '/defines.php';
}

So you can create your own constants file /defines.php and put your own constants in it.

You could use Joomla's core /includes/defines.php as example.

  • Is this solution "upgrade-proof"? IOW, it won't get erased upon upgrading the core? Is this best practice? How would you manage this as part of an installable/un-installable extension? – mickmackusa Dec 10 '19 at 20:58
  • 1
    @mickmackusa I would say that answer is upgrade proof as there is no define.php file provided by Joomla in the webroot that I can see. I think it would also be best practice because the Joomla is looking for the defines.php file so they are expecting this would be where any additional defines are made. There is not enough information in the OP as to whether this is the right place for their requirements or not as '...anywhere in the project' could mean Joomla or an extension. Maybe your last question is perhaps a new Question you could post on JSE in itself. :) – Irata Dec 11 '19 at 2:40
  • @Irata I used Ctrl+Shft+N to find default.php in PHPStorm, and I see that the defines.php file (unless I am misunderstanding terminology) is a core file. Do subsequent core upgrades not overwrite this file or anything in the includes directory? – mickmackusa Dec 11 '19 at 2:48
  • 2
    @mickmackusa - you probably misunderstood the answer a bit, since it talks about the Joomla root directory where you should create and place a new (own) file define.php. The core Joomla defines.php file in /includes directory is of course overridden at updates. You can just use that as an example file when you create your own in the root directory. – Zollie Dec 11 '19 at 5:07
  • 2
    yes, now that I've re-read the answer, you're right -- I misunderstood the advice. – mickmackusa Dec 11 '19 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.