Codigniter 2 - call function from extended class - php

First disclaimer - I have inhered project, and there is a lot of legacy code which I can't delete (I can delete it, but then I will need to spend few months to write everything from scratch). I extended all the models with custom MY_Model (it is extending the core) which have save function. Also most of old models have save function.
My question is this:
How can I call the save function from MY_Model, and not from the class that is extending MY_Model? Is it possible?

I am unsure about the implementation but when you want to call a static function in current class not the class that extends it you can use :-
self::save();
See it elaborated in https://stackoverflow.com/a/1189663/2489860

Related

PHP, how to effectively use the functions in a class

I am using codeigniter framework. let say i had controller class named myclass, this class have a function that i want to access from another controller class.
I got troubled when access the function from another controller class, i cant create an instant of this myclass class, it say class 'myclass' not found. i dont want to use include 'myclass.php'; because class myclass have __constructor method, i afraid the content inside __constructor method will conflict with another __constructor.
what the best solution for my case?
Thanks
CodeIgniter framework has a way to automatically loading your custom classes. Look here: http://www.codeigniter.com/userguide2/general/autoloader.html
Just make it possible for the framework to auto-load your class(es) and the problem is solved.

How to load function from library on page load using codeigniter

I am doing my project using codeigniter
I want load a function when the page loads. i tried to give the function in __construct() but i need to call the function at every page.
So i want to call the function which is in library without calling from __construct()
Can anyone give me the solution for my problem
Take a look into the hooks. You can configure a function or a method of a class to be executed in various places in the life cycle of every request, depending in your needs.
If you need the controller instance for your functionality (the $this in most contexts) you probably need the post_controller_constructor one, and use the get_instance() to get a hold of the controller instance inside of the hook, to load in libraries or call models and such.
An other way could be that you extend the CI_Controller class with a MY_Controller class, place it under application/core/MY_Controller.php and move your code inside that classes __construct and use the MY_Controller as the base class of your regular controllers, as described the Creating Core System Classes page.
Add a library in autoload, then in that library file use __construct to call your function.

cakephp components and helpers breaks oop logic! How should I proceed?

I have a string manipulation class that I need in views and in controllers also!
I saw that cake reuses code in Components and in Helpers for this type of situations which on my opinion breaks the OOP logic (eg. Session->read)!
Instead of doing this I created a vendor class which I imported in a StringsHelper and in a StringsComponent. I then created an identical function which instanciates the Vendor/String class and returns the results from the corresponding function. This is not quite inheritance and still redundant, but if I change code in my class it changes everywhere.
Is there a better way to do this?
You do not need to wrap this kind of class in a Helper or a Component.
You could simply create a class with static methods and put it in APP/Lib like mentioned by Mark.
<?php
class StringTool{
public static function manipulate($string){
...
}
}
and then use it in whatever class you need, wether in a Component, a Helper, a Model, etc.
<?php
$s2 = StringTool::manipulate($s1);
I asked this same question before. Best place is in app/Libs, where you can put a class with static helper functions that can be used anywhere in your application, including controllers and views.
Import the class using App::import('Lib', 'YourClass')
CakePHP - Where is the best place to put custom utility classes in my app structure?

CodeIgniter Controller Constructor

I'm very new to codeigniter ,
I wanted to know what is the meaning of a constructor in a controller . I saw the following code in a codeigniter tutorial -
class upload extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(form);
}
// rest of the class...
My question is when is the constructor invoked - is it called every time the controller serves a request (e.g the controller class is instantiated for each request it receives?)
Well, that's a more general PHP question. Anyway, yes, the magic method __construct() is called (automatically) upon each instantiation of the class, as you can see in the manual: http://www.php.net/manual/en/language.oop5.decon.php
Usually, in CI is not necessary to call a constructor, unless you actually want one. In the example you posted, the code loads the helper on every instantiation of the class - which is the same as loading the helper in every method, just saves a lot of typing and ensures it's not forgotten. You can alternatively put the library/helper/model you want to have alywas loaded in the respective autoload array in config/autoload.php (check "autoloading" in CI's manual)
Once you define a constructor in your child Controller you're compelled to call the parent constructor (of the mail CI_Controller class), because there is where the main CI object is created and all the classes are loaded, and you need those in your child controller too; if fail to do so your child class will construct separately and won't inherit.
I hope I made myself clear, english is not my mothertongue :)
the constructor is magic Literally its called a magic method.
what makes the constructor cool is that it will do things for you BEFORE any of the methods. So if you have an admin class, and someone should be logged in in order to access it - you can check for login in the constructor and bounce them out if they are not authorized.
in the constructor you can load the models, libraries, helpers, etc that your class needs, and they will be available for any method in the class.
you can load in variables that are used by methods. this is really useful for models.
Don't use _construct() function in latest apache & codeigniter
Use helperlin in index() function
That's a general question. Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.
$this->load->model('Model_name');
now when you write this line in your constructor you don't need to load this model again and again in your methods of that class.

create construct in class inherited by model or controller

I'm a java programmer and now I want to learn CodeIgniter framework to apply to my php application. I saw many examples on the web and I have a question. When I create a model or a controller, I have to inherit from CI_Model and CI_Controller but my question is: do I have to always create the construct in every model o controller? So I mean I have to put in every class
function __construct()
{
parent::__constuct();
}
If you don't override the __construct, it is not necessary. But if you override it, you need to call parent::__constuct();, php will not call the parent constructor automatically.
The answer is no.
I don't know how is it in java. But in php if no construct method is found in the child class it will call the parent one

Categories