Codeigniter - Cannot access property started with '\0' error - php

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.

Related

Call to undefined method stdClass::full_name()

I am trying to chain static functions, so I can use different viariables inside one another.
I have 2 functions like so:
<?php
namespace Adspace\Model;
use Adspace\Core\Request;
use Adspace\Core\DatabaseFactory;
use Adspace\Core\Alert;
use Adspace\Core\Session;
use Adspace\Core\Filter;
Class AccountModel
{
// the info to store user info in
public static $userInfo;
public static function getuser()
{
// the user's info
static::$userInfo = SELF::user_details(null, Session::get('user_id'));
return static::$userInfo;
}
public static function full_name()
{
// return the full name
return ucwords(Filter::XSSFilter($userInfo->user_firstname).' '. Filter::XSSFilter($userInfo->user_surname));
}
I am trying to use that like so:
<?php echo \Adspace\Model\AccountModel::getuser()->full_name(); ?>
But I am getting the following error:
Fatal error: Uncaught Error: Call to undefined method
stdClass::full_name() in
C:\xampp\htdocs\application\view\templates\back\header.php:48
What is causing this issue, and how would I fix this?

CodeIgniter: error loading custom library

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

Trying to get property of non-object in PHP

I'm working with PHP and get error message:
Notice: Trying to get property of non-object in D:\xampp\htdocs\gmbr\fw\model\UserModel.php on line 234
This is my code:
# Static user
public static function is_admin()
{
return self::$auth->admin?true:false; // <<- This line 234
}
public static function username()
{
return self::$auth->username;
}
Probably what's going on is that the $auth object isn't initialized yet when your method is called.
Use a var_dump(self::$auth) to see what's going on.
If this is the case, I would use a Singleton design pattern with $auth, Running a self::getAuth() instead of self::$auth.

Pass the current class as parameter

I have a class like this:
// file /models/person.php
class Person
{
public function create_path()
{
self::log();
path_helper($this); //a global function in other php file
}
public function log()
{
echo "trying to create a path";
}
}
This is the way how Person is instanciated:
//file /tools/Builder.php
include('/models/Person.php');
class Builder
{
public function build()
{
$type = 'Person';
$temp = new $type();
$temp->create_path();
}
}
As you note in Person class, I am calling the object in question with $this reference. But this is not correct because an error is showed:
Message: Undefined variable: this
I suppose that $this reference point to other object or it is unable to work because the object is created from another script. Also, I tried to use self because there was not problem calling methods with that, but as parameter I get:
Message: Use of undefined constant self - assumed 'self'
So, can you guide me to the right direction?
I tested your code out for myself, with a few minor changes. It appears to work properly.
Changed self::log() to $this->log()
Added global function path_helper (I have no idea what this does)
PHP
function path_helper(Person $object)
{
var_dump($object);
}
class Person
{
public function create_path()
{
$this->log();
path_helper($this); //a global function in other php file
}
public function log()
{
echo "trying to create a path";
}
}
class Builder
{
public function build()
{
$type = 'Person';
$temp = new $type();
$temp->create_path();
}
}
$Build = new Builder();
$Build->build();
Result
trying to create a path
object(Person)[2]
Your code is correct and your going in the right direction.
You should call the log method like this:
$this->log();
because using self:: is reserved for static methods.
Also, try calling the path_helper function like this:
path_helper(self);
Hope I could help you. Couldn't test it, but it should work.

Kohana 3.x call_user_func() error

i keep on having this error message on Kohana 3
ErrorException [ Warning ]: call_user_func() expects parameter 1 to be a valid callback, function 'pages_save' not found or invalid function name
inside my controller i have the following codes:
public function action_pages(){
return call_user_func('pages_'.$this->request->param('id'));
}
function pages_save(){
$this->auto_render = false;
}
if i would access admin/pages/save i should be redirected to pages_save() function right? but somehow i cant? Kohana keeps on throwing me the following exception stated above. how should i go with this. optimizing is one thing in my mind, if i do switch-case / if-else block it would take me forever if there are too many that i need to do...
First of all you should trust the error message. Which means you did an error. Clean up your code a little:
public function action_pages()
{
$method = sprintf('pages_%s', $this->request->param('id'));
return call_user_func($method);
}
public function pages_save()
{
$this->auto_render = false;
}
The changes here are only little: Added the missing visibility specifier to pages_save and put the methodname into a variable called $method.
Now it's getting more visible, that you used a public function name here which is not defined, instead of the class method you'd like to invoke on $this. So let's fix that:
public function action_pages()
{
$method = sprintf('pages_%s', $this->request->param('id'));
return $this->$method();
}
This should solve your issue.
It's not Kohana, it's PHP that is throwing the error. To use call_user_func() from within a class, you need to use the following syntax:
call_user_func(array($this, 'pages_'.$this->request->param('id')));

Categories