Call to undefined method stdClass::full_name() - php

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?

Related

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

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.

Using method from library included by use

I need to use method from Nette library, that I'm including by use command. But it doesn't work as I want to, throws fatal error, that I am calling undefined method.
How should I approach that method to make it work? Stupid question, but I am kinda new to OOP...
Method from class PresenterComponent.php
public function getPresenter($need = TRUE)
{
return $this->lookup('Nette\Application\UI\Presenter', $need);
}
And my code, where I need to use that method:
use Nette\Application\UI\PresenterComponent;
class DatabaseCollectionAdapter extends ArrayDataAdapter
{
// ..... some code......
$this->user = $this->getPresenter()->getUser();
Error:
Fatal Error
Call to undefined method Ctech\Gridator\DataAdapter\DatabaseCollectionAdapter::getPresenter()
change this
$this->user = $this->getPresenter()->getUser();
to this:
$object = new yourObject(); // yourObject extends PresenterComponent
$this->user = $object->getPresenter()->getUser();
use Nette\Application\UI\PresenterComponent; does not include or do any kind of magic to make it's functions available on the fly.
https://secure.php.net/manual/de/language.namespaces.importing.php
It's just a shorthand that helps you to use PresenterComponent directly without specifying the whole namespace.
Your DatabaseCollectionAdapter or ArrayDataAdapter has to have a function that looks like this:
class AdapterClass
public function getPresenter() {
return new Nette\Application\UI\PresenterComponent;
}
}
or something like this
use Nette\Application\UI\PresenterComponent;
class AdapterClass
public function getPresenter() {
return new PresenterComponent;
}
}

Why my getter (__get) is not called in PHP class?

I have read all related question, but failed to remove the bug from my code. Please guide me about possible error in my code.
When I try to call following code, it reports Error: Call to undefined method SessionManager::close() in E:\wamp64\www\mjs-cms\private\systemcore\helper\SessionManager.php on line 22 instead of "tried to call close".
Thanks in advance.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class SessionManager{
public function __construct() {
session_start();
}
public function is_exist($a){
return isset($_SESSION["system".$a]);
}
public function add($a,$b){
$_SESSION["system".$a]=$b;
}
public function addCookies($a,$b){
setcookie($a, $b, time() + (86400 * 30), "/"); // 86400 = 1 day
}
public function sessionKey(){
return session_id();
}
public function value($k){
if(!isset($_SESSION[$k]))
$this->close("SESSION_NOT_DEFINED".__LINE__);
return $_SESSION[$k];
}
public function __get($key)
{
echo "tried to call $key";
return get_instance()->$key;
}
}
__get method is for accessing undeclared properties of a class.
For calling undeclared functions is __call or __callStatic.
public function __call($method_name, $arguments)
{
echo "tried to call: $method_name";
}
If you want to use __get - you must call for undefined property. In this case it is not
SessionManager::close() // call method `close()`
It must be:
$sm = new SessionManager;
$sm->propertyName; // trying to access undefined property `propertyName` of an object
Take into consideration that
Property overloading only works in object context.
which means that trying to access static property like
SessionManager::staticProperty;
will not work with __get.

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.

PHP call to static function fails

All,
I'm getting an error with the code below. Here is the error message I get:
Notice: Undefined variable: userDAO in C:\wamp\www\Projetv0.2\Model_User.php on line 15
and then
Fatal error: Call to a member function checkRecordExists() on a non-object in C:\wamp\www\Projetv0.2\Model_User.php on line 15
The relevant code is below. What I try to do with the code is have a unique class (DAO_DBrecord) to access several tables in a db. In the case below, I want to access my users table. To do that I have created a specific static function createUserDAO inside the DAO_DBrecord class that calls the constructor with the right table name, users. However, it doesn't work, and I can't figure why.
Model_User.php:
<?php
require_once('Class_DB.php');
require_once('DAO_DBrecord.php');
class Model_user{ // Represents a connection to the users table in the DB
private $db;
private $userDAO;
function __construct($db){
$this->db=$db;
$userDAO=DAO_DBrecord::createUserDAO($this->db);// static function - calls constructor w/ 'user' table name parameter
$this->userDAO=$userDAO;
}
function userInfoExists($userInfo, $colName){
return $userDAO->checkRecordExists($userInfo, $colName);
}
//Other stuff
}
?>
DAO_DBrecord.php:
<?php
require_once('Class_DB.php');
class DAO_DBrecord {
private $db;
private $table;
private function __construct($db,$table){
$this->db=$db;
$this->table=$table;
}
public static function createUserDAO($db) {
return new DAO_DBrecord($db, 'users');
}
//Other stuff
}
?>
Thank you all for your help!
JDelage
That's not a problem with the static function. The problem is that PHP doesn't have an implicit $this. When you're referring to the member variable within the class (as you are in userInfoExists), you have to say $this->userDAO rather than just $userDAO.
Of course, all this assumes that the DAO_DBrecord class has or inherits a checkRecordExists function. If it doesn't, you're going to have other problems.

Categories