PHP basic OOP from Magic Function to Date Time

Choirul Anam
3 min readApr 8, 2022

Learning from Udemy

Photo by Markus Spiske on Unsplash

Assalamualaikum warrahmatullahi wabarakatuh.

Magic Function

magic function is a function that has been determined by PHP, we can’t create that function unless it’s purpose has been determined, in the previous material we have discussed some magic functions such as __construct() as constructor, __destruct() as destructor and __clone() as cloned objects.

__toString() function

The __toString() function is one of the magic functions that is used as a string representation of an object, if for example we want to create a string from an object we can create a __toString() function.

__invoke() function

This is a function that is executed when the object we create is considered a function, for example when we create an object
student(), the function __invoke() will be executed automatically.

__debugInfo() function
in the previous material we often debug variables using the var_dump() function, the var_dump() function actually calls the __debugInfo() function, if we want to change the contents of the debug info we can create a __debugInfo() function.

Overloading

Overloading is the ability to dynamically create properties or functions, overloading is closely related to magic functions.

Covariance and contravariance

when we override a function from the parent class, usually in the child class we will create a dunciton that is the same as the function in the parent. Covariance allows us to override the return from the parent with a more specific return value.

Contravariance
allow a child class .to be unspecified.

contravariance Child becomes parent, while covariance parent makes child.

Date Time

to manipulate time in PHP we can use DateTime, there are lots of functions in the Date Time class that we can use to manipulate time data.

Date Interval
we can manipulate some date, or time data

Date TimeZone
when we create a DateTime it will automatically create a time with the timezone that is set in the configuration in the php.ini file
For example, we will change the timezone, we can use the function set TimeZone Date Time.

Date Time Format

if we want to create a string representation of the DateTime that we have created we can use the format() keyword, the format() function accepts an argument in the form of a format string, which can be used to manipulate the way we display the time format string.

Parse Date time
opposite of date time.

thats for today. Thanks

Wassalamualaikum warrahmatullahi wabarakatuh.

--

--