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
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
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..
You can create a function in any opencart library (system/library/)
As example in system/library/document.php
function something() {
$string = 'Hello World';
return $string;
}
And use anywhere in openсart as
$something=$this->document->something();
P/s code in header.tpl will not work in ajax or direct request
Matricore's answer worked perfectly for me, but I also had to construct the new class with the config and db available.
Code below:
class yourclassname {
public function __construct($registry) {
$this->config = $registry->get('config');
$this->db = $registry->get('db');
}
}
You can then run queries via $this->db->query("Your query here");
Related
I am having a really weird error, I have this template Engine (which I think is working correctly since it shows most files correctly):
<?php
class Template
{
protected $template;
protected $vars = array();
public function __construct($template)
{
$this->template=$template;
}
public function __get($key)
{
// TODO: Implement __get() method.
return $this->vars[$key];
}
public function __set($key, $value)
{
// TODO: Implement __set() method.
$this->vars[$key]=$value;
}
public function __toString()
{
extract($this->vars);
chdir(dirname($this->template));
ob_start();
include basename($this->template);
//echo $this->template;
return ob_get_clean();
}
}
Then I have this file named topic.php with the following code in it:
<?php
require ('core/init.php');
$template = new Template("templates/topic.php");
echo $template;
?>
And my template "templates/topic.php" looks like this:
<?php
echo "Testo";
And finally I have this other file "templates/test.php" which looks like this:
<?php
echo "Testing";
If in line two of topic.php I change $template = new Template("templates/topic.php"); to $template = new Template("templates/test.php");
It just prints correctly, "Testing" shows in the webpage but if I want to show "templates/topic.php" it doesn't show anything in the webpage. Also if I refactor "templates/topic.php" to "templates/topici.php" for example, it works fine.
This is not a project I need to deliver and the workaround is really simple... is not to use the file name topic.php in the template directory, but I want to understand what is happening.
Note: Also, I have other files with a similar structure where for example the file is named "create.php" and the file it is supposed to show is "templates/create.php" and they all work fine.
Thanks for your help.
I am using the Slim framework for the development of a web-app.
However, I came across some issues which I can not solve.
I want organize my code in classes and call certain methods from the classes.
i have an index.php file in which the following function exists:
$app->post('/', function () use ($app) {
// some code here
//a variable $result I want to get the result from a method of the class Generate_num
$result = (here I want it to take as a result the function "generate" from a .class.php file which I have stored in a special folder "classes"
//another code
});
my class code looks like this
class Generate_num
{
public static function generate()
{
//some code
}
}
Any suggestions ? Thank you !
Given this class:
class Generate_num
{
public static function generate()
{
//some code that creates $number
return $number;
}
}
You call it like this:
$number = Generate_num::generate();
I'm working on WordPress plugin where I have 2 php files.
class.php has code as:
class Plugin_Name {
public function say_hello() {
echo "Hello";
}
}
Now I want to call this say_hello() function from another welcome.php file.
I tried
$hello_function = new Plugin_Name();
$hello_function->say_hello();
But it doesn't work. Is there any way to call public function from another php ?
You need to include the first function in the other file, that way the other file knows the code is there. At the top of welcome.php add
require_once('/path/to/class.php');
$hello_function = new Plugin_Name();
$hello_function->say_hello();
$this->admin_model->list_user()
I am writing most of the time my programs in OOP PHP. but I am writing like this...
$this->hello_world().
The above code is CodeIgniter and I think CakePHP also following same coding style.
Please give me simple example how to make my "hello_world" like
$this->something->hello_world().
Thanks you on advance.
Surya
Its nothing special; $this->admin_model is a property which contains an object, and for all purposes is identical to $object->method();
A step by step would look like:
$this->property = new MyObjectWIthADoItMethod();
$this->property->DoIt();
something is just an object of a type which has hello_world() method.
So:
class Something
{
public function hello_world()
{
echo 'Hello, big world!';
// Do work.
}
}
class Program
{
private $something;
public function Run()
{
$this->something = new Something();
$this->something->hello_world()
}
}
$program = new Program();
$program->Run();
I am working on creating my own very simple MVC and I am brainstorming ways to go from the controller to the view. Which involves sending variables from a class to just a plain old PHP page.
I am sure that this has been covered before, but I wanted to see what kind of ideas people could come up with.
//this file would be /controller/my_controller.php
class My_Controller{
function someFunction(){
$var = 'Hello World';
//how do we get var to our view file in the document root?
//cool_view.php
}
}
Some kind of hashtable is a good way to do that. Return your variables as association array which will fill all the gaps in your view.
Store your variables as a property in your controller object, then extract them when rendering
class My_Controller {
protected $locals = array();
function index() {
$this->locals['var'] = 'Hello World';
}
protected function render() {
ob_start();
extract($this->locals);
include 'YOUR_VIEW_FILE.php';
return ob_get_clean();
}
}
You can define those magic __get and __set methods to make it prettier
$this->var = 'test';
I'm also developing my own simple MVC and the most simple way to do it is ...
class My_Controller
{
function someFunction() {
$view_vars['name'] = 'John';
$view = new View('template_filename.php', $view_vars);
}
}
View class
class View
{
public function __construct($template, $vars) {
include($template);
}
}
template_filename.php
Hello, <?php echo $vars['name'];?>
I highly recommend you to take a look at PHP Savant http://phpsavant.com/docs/
I'd checkout Zend_View and how it accomplished view rendering.
You can get the source of View and AbstractView on github - unfortunaly I don't find the current repository (in svn) that easy to browse.
Essentially the view variables are contained in a View object (which your controller would have access to), then the template (plain old php document) is rendered inside that object. That method allows the template access to $this.
It would be something like:
<?php
class View
{
public function render()
{
ob_start();
include($this->_viewTemplate); //the included file can now access $this
return ob_get_clean();
}
}
?>
So in your controller:
<?php
class Controller
{
public function someAction()
{
$this->view->something = 'somevalue';
}
}
?>
And your template:
<p><?php echo $this->something;?></p>
In my opinion this pattern allows you much flexibility with the view.
I created my own MVC for the free PHP course I'm conducting for a handful of people wanting to get better at PHP.
By far the best way to do this is to use the Command + Factory pattern.
E.g.
interface ControllerCommand
{
public function execute($action);
}
In each controller:
class UserController implements ControllerCommand
{
public function execute($action)
{
if ($action == 'login')
{
$data['view_file'] = 'views/home.tpl.php';
}
else if ($action == 'edit_profile')
{
$data['view_file'] = 'views/profile.tpl.php';
$data['registration_status'] = $this->editProfile();
}
return $data;
}
}
From your main front controller:
$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;
The point is that every Controllercommand class has an execute function and that returns its view and any data for that view.
For the complete MVC, you can access the open source app by emailing me at theodore[at]phpexperts.pro.