Magic Constants and Magic Functions in PHP

In this article we will explore Magic Constants and Magic Methods, how they are useful to us? and what exactly they are?

easy solution web : OOPS in PHP

In this article we will explore Magic Constants and Magic Methods, how they are useful to us? and what exactly they are?

So let’s understand Magic Constants first.

Magic Constants

Magic Constants are those constant whose value is going to be defined in the time of compilation. That is why they are also defined as Compile Time Constant.

There are nine magical constants that change depending on where they are used. For example, __FILE__ value depends on which file you are calling constant. It will give that filename. If you again call it in some another file its value will be changed and this time it will give the filename where you have accessed it.

This is magic of these type of Constants.

 

Name Description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name.
__CLASS__ The class name. The class name includes the namespace it was declared in. Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in.
__METHOD__ The class method name. (It will return function name along with the classname)
__NAMESPACE__ The name of the current namespace.
ClassName::class The fully qualified class name. See also ::class.

These Constants we use when we need to debug or we are creating a logging system in our application. So that we will be able to track the process is going as we thought or if something went wrong we will be able to track it.

I hope whenever you are creating your logging mechanism you will use these Magic Constants.

 

Now let’s discuss Magic Methods.

Magic Methods

There are methods which start with “__” in PHP classes which are magical in nature. If you want these magic should be associated with them so do not define methods as defined below:

 

Name Description
__construct() It gets called when an object instantiated.
__destruct() When object is not referenced by any other variable then this will be called.
__call() __call() is triggered when invoking inaccessible methods in an object context.
__callStatic() __callStatic() is triggered when invoking inaccessible methods in a static context.
__get() __get() is utilized for reading data from inaccessible properties.
__set() __set() is run when writing data to inaccessible properties.
__isset() __isset() is triggered by calling isset() or empty() on inaccessible properties.
__unset() __unset() is invoked when unset() is used on inaccessible properties.
__sleep() use of __sleep() is to commit pending data or perform similar cleanup tasks.
__wakeup() __wakeup() is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.
__toString() The __toString() method allows a class to decide how it will react when it is treated like a string
__invoke() The __invoke() method is called when a script tries to call an object as a function.
__set_state() This static method is called for classes exported by var_export() since PHP 5.1.0.
__clone() An object copy is created by using the clone keyword (which calls the object’s __clone() method if possible)
__debugInfo() This method is called by var_dump() when dumping an object to get the properties that should be shown.