Menu Close
    Start a Project Request quote Reading list Switch to dark mode

    Using PHP Traits with Opencart

    According to PHP documentation, Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

    We must keep in mind that a Trait can not be instantiated. Neither it can extend a class nor a trait itself. The best part of Traits is that its members can be accessed without using inheritance.

    The basic syntax of Traits:

    We can easily use the trait by simply write the trait name after the ‘use’ keyword in a class. For example:

    We can use the Traits in the Opencart as well. A simple example to show how it works in the Opencart.

    Now, here’s the example of the application of the Trait in the Opencart. This is an example to convert the currency using the Google’s API.

    So, in order to use the trait, we have created a file with the name trait.php and put this code to it.

    Now, we will use this Trait in our Opencart controller’s class. Here’s the code to that.

    So, here we end up with a few examples of traits and also its application with the Opencart.

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again