Back to Top

Using PHP Traits with Opencart

Updated 26 April 2023

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:

<?php
trait traitName {
    public function method1() { // user defined method
        // something
    }
    public function method2() { // user defined method
        // something
    }
}
?>

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

<?php
class Test {
  use traitName;
}
?>

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

Searching for an experienced
Opencart Company ?
Find out More
<?php
/**
 * Webkul Software.
 *
 * @category Webkul
 * @package PHP Concepts
 * @author Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
trait traitExample {
    public function common1() {
        echo 'Something common1';
    }
    public function common2() {
        echo 'Something common2';
    }
}
class ControllerTestTest extends Controller { // class declaration in the Opencart
    use traitExample; // using trait
    public function index() {
        $this->common1(); // calling the trait's method
    }
}
?>

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.

<?php
/**
 * Webkul Software.
 *
 * @category Webkul
 * @package PHP Concepts
 * @author Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
trait traitCurrency {
    public function convertCurrency($from_currency, $to_currency, $amount) {
      if($from_currency == $to_currency) {
        return round($amount, 2);
      }
      $amount = urlencode($amount);
      $from_currency = urlencode($from_currency);
      $to_currency = urlencode($to_currency);

      $url = "http://www.google.com/finance/converter?a=$amount&from=$from_currency&to=$to_currency";

      $ch = curl_init();
      $timeout = 0;
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

      curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $rawdata = curl_exec($ch);
      curl_close($ch);
      $data = explode('bld>', $rawdata);
      $data = explode($to_currency, $data[1]);

      return round($data[0], 2);
    }
}
?>

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

<?php
/**
 * Webkul Software.
 *
 * @category Webkul
 * @package PHP Concepts
 * @author Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */
include 'catalog/controller/test/trait.php'; // first, including the file

class ControllerTestTest extends Controller {
    use traitCurrency;
    public function index() {
        $converted = $this->convertCurrency('USD', 'INR', 50); // 
        print_r($converted);
    }
}
?>

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

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home