TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Defining Constants for use in application
  • Hi all, new to Kohana and impressed with its functionality so far...

    My question is about defining constants. I want to be able to define constants for all of my table names in the database that I can refer to throughout the code, so that if I ever change the names of my tables I only have to change them in a single place. Where do I do this? It doesn't seem to make sense that there's nowhere to do it, and a search for this topic on the forums and the user guide didn't turn up anything useful.
  • I might make them options in database.php config file, then reference them with normal config calls.

  • well, I've already listed them all in the database.php config file using

    define('CONSTANT','tbl_name')

    but I was wondering if there might be a more "correct" way of doing it. I've also got access levels throughout the application that I had previously defined in a similar way, so I'll need to work out where to put those too. On most apps I worked on pre-frameworks, I would just use a constants.php file and fill it up with all of these as appropriate, and include it in the header of the page.
  • Class constants are always a better idea than global constants.

  • granted, but when it's something like table names that you're going to use in almost every model throughout your application, it's nice to declare once and then not have to worry about them anymore. I'd still have to declare them in each class, or use inheritance and grab them all from the parent model anyway.

    I'll make do with my current approach for now, and think about a more elegant solution next time.
  • Kohana configuration API does exactly what you need: http://docs.kohanaphp.com/core/kohana#methods_config

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion