Browsing Category PHP

PHP Code snippets can be found here.

PHP Class Customization trait

I’ve written a trait to help with customizing classes behaviors. For example if you have a class A with method getName() but in this method you want to check whether you want the full name to be returned or just the last or first name, the solution will be creating a new method or editing the code. This trait solves it

  All you need to do is to add use HasOptions to the desired class  

Usage:

  Note: In order to use traits and customize the $options array, the class where you’re adding your $options array needs to a child of the class where HasOptions is used, so the example i gave you will generate error related to $options = []; [alert type=”warning”]If a trait defines a property then a class can not define a property with the same name unless it is compatible (same visibility and initial value), otherwise a fatal error is issued….

Read More

PHP: How to get Current Date

There are multiple ways which can be used to get PHP Current Date, I’m going to start with the simplest way. the date will return a string

According to PHP Manual, date() takes two parameters, the first one is format the second one is timestamp.