
Change the aspect ratio of an image using CSS
24/09/2021In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
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
In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Here is how you can fix node incompatibility error, using "Node Sass does not yet support your current environment" as an example.
Need to make a directory in PHP? No Problem! mkdir has you covered... Need to recursively make directories in PHP? mkdir has you covered, too!
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!