Create, register and use shortcodes in WordPress
18/03/2023Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
There are 9 ‘Magic Constants’ that developers can use in PHP. Unlike PHP ‘Predefined Constants’, ‘Magic Constants’ are ambiguously named ‘constants’ which change based on where they are used within your script. Some of these are more useful than others, and I imagine that you will have already seen and used ‘__FILE__’ and ‘__DIR__’ on a regular basis.
Magic constants can be useful for many things, from debugging to file management. Here are some simple examples showing how to use each ‘Magic Constant’.
Note: All of these ‘Magic Constants’ get resolved at compile time instead of runtime.
This constant returns the currently executed line number within your script.
1. <?php
2. echo __LINE__; // Result = 2
3. echo __LINE__; // Result = 3
We can use the ‘__File__’ magic constant to return the full path of the file being executed.
echo __FILE__; // Result = "/full/path/of/this/file.php"
If we wanted to include the example above in a separate file, it would return the path of the included file and not the path of the file we include the file in.
include "include.php"; // Result = "/full/path/to/include.php"
This useful ‘Magic Constant’ will return the full directory path of the currently executed file.
Note: ‘__DIR__’ will not return a trailing slash.
echo __DIR__; // Result = "/full/directory/path/of/file"
Like ‘__FILE__’, if we use ‘__DIR__’ in an included file, it will still return the directory of the included file.
include "/some/path/to/include.php"; // Result = "/some/path/to"
Returns the name of the function we are currently calling.
function helloFunction() {
echo __FUNCTION__;
}
helloFunction(); // Result = "helloFunction"
Returns the name of the class that we are currently using.
class HelloClass {
public function helloMethod() {
echo __CLASS__;
}
}
$x = new HelloClass;
$x->helloMethod(); // Result = "HelloClass"
This will return the name of the trait that we are currently using. It will also prepend the trait with the current namespace as well.
namespace HelloNamespace;
trait helloTrait {
function helloMethod() {
echo __TRAIT__;
}
}
class HelloClass {
use helloTrait;
}
$x = new HelloClass;
$x->helloMethod(); // Result = "HelloNamespace\helloTrait"
This ‘Magic Constant’ returns the name of the method that we are currently calling, in relation to the containing class. This will return the name of the class followed by the name of the method.
class HelloClass {
public function helloMethod() {
echo __METHOD__;
}
}
$x = new HelloClass;
$x->helloMethod(); // Result = "HelloClass::helloMethod"
Returns the name of the current namespace in use.
namespace HelloNamespace;
echo __NAMESPACE__; //Result = "HelloNamespace"
Because ‘ClassName::class’ returns the fully qualified class name, it is really handy when using namespaces.
namespace HelloNamespace;
class HelloClass {}
echo HelloClass::class; // Result = HelloNamespace\HelloClass
Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.
Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.
In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.
Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.
Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.
Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!