CakePHP Global Constants and Functions

Published on Sep 21, 2012 by Jamie Munro

If you ever find yourself perusing some of the default code that comes with CakePHP, you might find yourself somewhat curious and confused when you see such functions as <?php e('Hello World');?> or <?pho echo h('<a href="">Hello World</a>');?>.

I know I certainly was and became determined to understand what the heck these were doing.  It quickly became apparent that these were just two of the many built-in extension functions that are part of the CakePHP framework!

 These two functions are probably the most common functions; the e function is a short cut for the echo function.  Meanwhile the h function is another short cut, but this time for the htmlspecialshars function.  I'm sure the amount of time the framework developers had to type htmlspecialchars they quickly decided to build a full set of these functions!

Starting of the alphabetically list of global functions are a variety of functions related to localization.  The first being the double underscore (__).  This is followed by several other localization functions that help customize specific actions, e.g. select from a specific category or domain.

Similar to the e function, there are two similar functions for outputting data.  debug() allows you to output a debug message that will display the file and line number; useful for understanding the order of executed code.  There is also a pr function that implements the print_r() function.

Next there are several functions for dealing arrays, environment variables, stripping slashes, checking if files exist, etc…

Along with these useful functions, there are also several useful constants that are defined that contain useful file path information as well as several more constants relating to timing values.  E.g. the number of seconds in an hour or year, etc…

If you are interested in seeing the full list as well as the full descriptions navigate over to the CakePHP Cookbook:

This link is explicitly for CakePHP 2.x; however, you can quickly find links for older versions as well.

Tags: CakePHP | php

Related Posts

blog comments powered by Disqus