I created a custom Library in CodeIgniter, and positioned it in application/libraries/VarMatrixSpecanimal.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class VarMatrixSpecanimal {
protected $variabiliMatrix;
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
public function get_matrix() {
return $this->variabiliMatrix;
}
}
?>
Then in one controller (application/controllers/certificate.php) method I added these two lines of code:
public function save1()
{
//..... some more code before
$this->load->library('VarMatrixSpecanimal');
$numerical_values = $this->varmatrixspecanimal->get_matrix();
//..... some more code after
But when I call save1 method, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Certificate::$varmatrixspecanimal
Filename: controllers/certificate.php
Line Number: 139
I don't understand where I do wrong, please help me.
I checked also CodeIgniter help http://www.codeigniter.com/user_guide/general/creating_libraries.html but I was not able to get my error
Add get_instance() function in constructor in VarMatrixSpecanimal.php
public function __construct()
{
$this->variabiliMatrix =& get_instance();
}
After this load library on your controller
$this->load->library('varmatrixspecanimal');
I have experienced anything like this because I forgot this line of code:
parent::__construct
in my __construct or constructor.
Add include file with correct path at the top of program will solve this issue.
include "../../VarMatrixSpecanimal.php";
Try to change your constructor from this:
public function __construct() {
$variabiliMatrix['cat']['no']=1;
$variabiliMatrix['dog']['a']=2;
$variabiliMatrix['bird']['b']=3;
}
to this:
public function __construct() {
$this->variabiliMatrix['cat']['no']=1;
$this->variabiliMatrix['dog']['a']=2;
$this->variabiliMatrix['bird']['b']=3;
}
Also, when loading your library i think you don't need to uppercase any letters
Related
I am calling function form library in codeigniter and it's giving me below error
PHP Fatal error: Cannot access property started with '\0' in /system/core/Exceptions.php on line 85
Code:
$this->load->library('test_library');
TEST_LIBRARY::first();
Class file:
class TEST_LIBRARY
{
public function first(){
return "here";
}
}
However, when I call the function using this method $this->test_library->first(); it's working fine.
It was working both ways before not sure what's going on. There is no other log messages in error.log file. How can I debug further and fix this issue?
Function first is not static but you called as if it is. Change test_libaray.php to :
class TEST_LIBRARY {
public function __construct() {}
public function first() {
return "here"; // i suggest to use __METHOD__ or __LINE__ instead
}
}
And then try:
$test_library = new TEST_LIBRARY();
$test_libaray->first();
instead of:
TEST_LIBRARY::first();
Or you can just change first static.
I am trying to load a model in a library.
This is how I am loading it,
class Init{
private $CI;
function __construct()
{
$this->CI = &get_instance();
$this->CI->load->model("backend/StoreSettings_Model");
}
}
When I try to access the method in the storesettings_model, I get the following error Undefined property: Init::$Storesettings_Model
public function settingsSetUp($store_name){
$settings = $this->CI->storesettings_model->getStoreSettings($store_name);
}
I have tried the changing the case to match the case of the class I am loading to StoreSettings_Model... still same problem
If I load the library with all lower case then it will work, but then when I upload to my development server which runs on Linux, it will throw an error because the class name is StoreSettings_Model and the im instantiating with storesettings_model
Has anyone face this issue before? If so what do you suggest about fixing it?
Any help would be really appreciated.
Try this
function __construct()
{
# $this->CI = &get_instance(); < ---- remove this
$this->load->model("backend/StoreSettings_Model"); # remove CI->
}
in Method
public function settingsSetUp($store_name){
$settings = $this->storesettings_model->getStoreSettings($store_name); # remove CI-> on here too
}
strange problem here. Using Yii framework I have the following class
class HtmlTableUi
{
public function __construct(CWebModule &$module,$data,$actions)
{
//...code goes here...
}
protected function renderTable()
{
//... code goes here ...
}
}
I can call HtmlTableUi::renderTable() from instance of HtmlTableUi in SchedulerModule class which is the main class in a separate application module. My SchedulerModule.php file:
<?php
Yii::import('scheduler.components.HtmlTableUi');
class SchedulerModule extends CWebModule
{
public function init()
{
parent::init();
}
public function beforeControllerAction($controller, $action)
{
return true;
}
public function printUI($data,$actions,$submitPath)
{
$ui = new HtmlTableUi($this,$data,$actions,$submitPath);
$ui->renderTable();
}
}
Here comes the tricky part - when I call SchudulerModule::printUI from view (index.php) this way
<?php
$this->module->printUI($casino,$actions,null);
?>
the code flow goes through SchudulerModule::printUI until reaching the point where renderTable is invoked ($ui->renderTable();) and instead stepping at the first line inside the body of that method it, contrary to any logic and rules, surprisingly jumps to CHttpSession::close !!?
Notice the Call Stack before invoking renderTable
protected/modules/scheduler/SchedulerModule.php.SchedulerModule->printUI:153
protected/modules/scheduler/views/default/index.php.require:21
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderInternal:127
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderFile:96
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->renderPartial:870
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->render:783
protected/modules/scheduler/controllers/DefaultController.php.DefaultController->actionIndex:57
and after:
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CHttpSession.php.CHttpSession->close:134
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CHttpSession.php.SchedulerModule->printUI:0
protected/modules/scheduler/views/default/index.php.require:21
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderInternal:127
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CBaseController.php.CBaseController->renderFile:96
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->renderPartial:870
/home/default/workspace/src/vlt/web/yii-1.1.12.b600af/framework/web/CController.php.CController->render:783
protected/modules/scheduler/controllers/DefaultController.php.DefaultController->actionIndex:57
Has anyone had similar issue? Can anyone explain that behavior?
I found the solution!!! The access modifier of renderTable is supposed to be public, not protected. This wrong somehow in Yii framework results in unexpected runtime behavior instead of compilation error.
I have been trying to implement the solution referenced at:
Error when trying to load view in my_controller
Don't know why, but Aptana indicates a syntax error on my line:
parent::load->view('common/header_out');
...which is seconded by the interpreter when I execute: "unexpected T_OBJECT_OPERATOR"
EDIT: Previously I tried using:
$this->load->view('common/header_out');
...which generated the following errors:
Warning: include(application/errors/error_php.php) [function.include]: failed to open stream: No such file or >directory in /home/uom2/www/system/core/Exceptions.php on line 167
Warning: include() [function.include]: Failed opening 'application/errors/error_php.php' for inclusion >(include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/uom2/www/system/core/Exceptions.php on >line 167
I am using CI 2.0.
Any help would be appreciated.
application/core/Uom_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Uom_Controller extends CI_Controller
{
public $data = array(); // Array to store data - passed to views.
protected $view_path = null; // Here to overide the view path if n
public function __construct()
{
parent::__construct();
}
protected function check_session()
{
//Here goes your function
}
protected function render() {
$data['page_title'] = 'Your title';
$data['page_title'] = 'Your title';
parent::load->view('common/header_out');
parent::load->->view('home/home', $data);
parent::load->->view('common/foot');
echo "view rendered: ".date('Y-m-d H:i:s');
}
}
controllers/home/home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends Uom_Controller {
function __construct()
{
parent::__construct();
// Set the view path manually for this method.
$this->view_path = 'home/home.php';
}
function __destruct() {
$this->render();
}
public function index()
{
echo "home updated";
}
public function out()
{
echo "logout";
echo "<br>";
echo $this->view_path;
echo "<br>";
}
}
obviously the intention is similiar to the SO post at the top of this post: I want the $this->render(); to be called and render my page header, body, and footer.
(My next task is to add the authentication check to this MY_Controller pattern, so I am hoping that whaever solution works for calling views also works on models.
Thanks everyone and I really have spent hours trying to find a solution to this beforw once again turning to SO.
I think your problem is you are calling parent::load->view instead of $this->load->view
Ugh. here is the problem:
function __destruct() {
$this->render();
}
According to a remark by Phil Surgeon on here
__destruct does not work as expected due to the way everything is
inherited.
...and further down the page, a more detailed explanation that seems to make sense:
The problem is that the controller IS the CI superobject, once you
destroy that, you lose all access to CI. And you don’t have any
control over the destruct process, so you don’t know what is still
accessable, and what isn’t.
Using __destruct() for anything other than cleanup actions for
destruction of the current object is a very bad idea…
The short answer is that things work so long as you are not calling a method from the extended class from "_destruct".
Hopes this helps someone else.
I have a problem, everything was working but then I tried to put my functions in my own libraries (to use them in different controllers) and it doesn't work.
I have SIGNUP controller with this:
$this->load->library('Check_functions');
// We check the form
$return_verif_form_signup = $this->check_functions->verif_form_signup($language);
which calls my librarie Check_functions:
class Check_functions {
public function verif_form_signup($language) {
if ($this->input->post()){
// Verification rules
$this->form_validation->set_rules('name', 'lang:name', 'trim|required|xss_clean');
....
if ($this->form_validation->run($this)) {
extract($_POST);
...
...
}
But I get the error:
Fatal error: Call to a member function post() on a non-object
Does anyone know how I could fix it?
thanks!
EDIT:
I have found the problem, the callback function is not called. If I replace callback_free_email by REQUIRED and I don't enter an email, my form is not submitted, so it's okay.
But if I have the following code, my form is always submitted. So the callback function is never called...
This is my code (i'm using HMVC):
class Check_functions {
private $CI;
public function __construct(){
$this->CI =& get_instance();
}
public function verif_form_signup($language) {
if ($this->CI->input->post()){
$this->CI->form_validation->set_rules('name', 'lang:field_name', 'trim|required|min_length[3]|max_length[25]|xss_clean');
$this->CI->form_validation->set_rules('email_signup', 'lang:field_email', 'callback_free_email');
...//other rules
if ($this->CI->form_validation->run($this->CI)) {
.....
}
}
}
public function free_email($str) {
return FALSE; // I have temporarly set that so I see if my function is called
}
}
I have a file called MY_Form_validation.php as suggested here: http://codeigniter.com/forums/viewthread/143057/#769347
class MY_Form_validation extends CI_Form_validation{
function run($module = '', $group = ''){
(is_object($module)) AND $this->CI = &$module;
return parent::run($group);
}
}
I really don't know what's wrong... why my callback function is not called?
Thank you for your help!
when you are writing libraries, you have to manually grab the Codeigniter instance like this
$CI =& get_instance();
then you would use $CI where you would normally use $this to interact with loaded codeigniter resources
so...
instead of
$this->input->post();
you would write
$CI->input->post();
Docs explain it here http://codeigniter.com/user_guide/general/creating_libraries.html
EXAMPLE LIBRARY STRUCTURE
class Examplelib {
// declare your CI instance class-wide private
private $CI;
public function __construct()
{
// get the CI instance and store it class wide
$this->CI =& get_instance();
}
public function lib_function()
{
// use it here
$this->CI->db->etc()
}
public function another_func()
{
// and here
$this->CI->input->post();
}
}
I finally found a workaround, instead of using the callback in my rule, I do a test later and call a verification function.
It’s works well like that.
Thanks for your help!