Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global function in opencart

Tags:

php

opencart

I'm trying to create simple function that can be executed on any page.

function something() {
    $string = 'Hello World'; 
    return $string;
}

Let's say I'm in the category page, I would just call $a = something(); and it would return my value

Platform : OpenCart

P.S. I'm still studying MVC architecture

like image 787
rusly Avatar asked Dec 13 '25 21:12

rusly


2 Answers

Since you are wanting to understand and learn about the MVC system, the correct way to go about this would be to create your own helper file and put it in /system/helper/ and then add the helper to system/startup.php. Take a look at how the json.php / utf8.php are done in these as a guide

like image 164
Jay Gilford Avatar answered Dec 15 '25 13:12

Jay Gilford


Create a new file to system/library/yourclassname.php with same code.

Also please add a class name to your function as below:

class yourclassname {

  function something() {
    $string = 'Hello World'; 
    return $string;
  }
}

And add it to your index.php file as below;

$registry->set('yourclassname', new yourclassname($registry));

Finally add it to your startup.php file as below:

require_once(DIR_SYSTEM . 'library/yourclassname.php');

You can use it anywhere with $this->yourclassname->something();

Thats All..

like image 23
Matricore Avatar answered Dec 15 '25 12:12

Matricore