Constants in config files?

121 views
Skip to first unread message

Nuwanda

unread,
Aug 30, 2017, 2:00:10 AM8/30/17
to Fat-Free Framework
Possible? Or do I have to define a constant from the config value?

ved

unread,
Aug 30, 2017, 2:11:31 PM8/30/17
to Fat-Free Framework
Not sure but I don't think so.
The docs don't say anything about it so I guess you'd have to define them after getting the values from the config.

ikkez

unread,
Aug 30, 2017, 4:10:50 PM8/30/17
to Fat-Free Framework
the config file is meant to configurate the framework and its HIVE.. not to transform php coding into text files.

Nuwanda

unread,
Aug 30, 2017, 5:24:49 PM8/30/17
to Fat-Free Framework
No, that's not right.

You can certainly set framework variables (debug, autoload, etc.) but the docs make it clear that config files are just as much for user settings. There are even custom settings sections available.

Richard Catto

unread,
Aug 31, 2017, 12:04:29 PM8/31/17
to Fat-Free Framework
What are you wanting to achieve?

The config file is used to create entries in f3's HIVE (array) which you can access using the $f3->get() method, with:

$f3 = \Base::instance();

No PHP variables or constants are set in the config files, only Hive entries.

You will have to define constants in php script. I do this in my index.php, but you could define them in a separate file constants.php and include / require the file into your index.php

Nuwanda

unread,
Aug 31, 2017, 4:44:31 PM8/31/17
to f3-fra...@googlegroups.com
But not all features of F3 are documented

indeed there're way to do that. But the simplest would be to define a custom array in your config file and iterate over that afterwards

[constants]
FOO
= Bar
VERSION
= 1.0

index.php
foreach ($f3->constants as $key => $val)
  define
($key, $val)

that's simple.. if you want it more complicated, you can write a custom config section parser, like described in this post: https://groups.google.com/d/msg/f3-framework/1kF2r0cPtvM/Iurz0drGCgAJ






Richard Catto

unread,
Sep 1, 2017, 1:47:44 AM9/1/17
to Fat-Free Framework
wow

On Thursday, August 31, 2017 at 10:44:31 PM UTC+2, Nuwanda wrote:
What I'm wanting to achieve is pretty obvious: a convenient place to set constants as well as other configuration values. I had hoped the F3 config files were such a place.

The tone in your reply suggests I'm new to F3. I'm not. I'm a developer and know my way around PHP as well as anyone. But not all features of F3 are documented--and you should know that--so I asked a question. And it was a perfectly reasonable question.


Nuwanda

unread,
Sep 2, 2017, 4:23:34 AM9/2/17
to Fat-Free Framework
That's a good solution. Thanks.

Nuwanda

unread,
Sep 2, 2017, 4:24:39 AM9/2/17
to Fat-Free Framework
Not "wow". But I got a helpful reply anyway.

Richard Catto

unread,
Sep 2, 2017, 5:09:00 AM9/2/17
to Fat-Free Framework
It's a solution, but I don't see any merit in resorting to this. As was previously said, fat free, is not a replacement for writing regular php. If you want to define constants, the easiest is to either put a block of them at the top of your index.php or put them in a file named something like constants.php and just list the define statements one after another.

Constants are typically things set by developers, whereas config files are meant to be edited by the installer or user (of your software). Typically, you do not wish to expose constants to being changed by anyone except a developer.

The entire purpose and efficiency of config files, is that you do not have to process them manually. You call $f3->config() to do that and everything is imported into the HIVE. Now you want to put some overhead on top of that. Remember, this overhead will have to execute for every page you load. The fat free concept is to be as lean as possible. Iterating over a HIVE array to create constants adds fat.
Reply all
Reply to author
Forward
0 new messages