CodeIgniter Forums
Does the error "should not be called statically" mean my namespace or use is wrong? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Does the error "should not be called statically" mean my namespace or use is wrong? (/showthread.php?tid=81089)



Does the error "should not be called statically" mean my namespace or use is wrong? - castle - 01-23-2022

Hi,
I'm confused here. How do I call a method into a controller from another Controller?
PHP Code:
<?php
/**
 * In strict mode, only a variable of exact type of the “type declaration”
 * will be accepted, or a TypeError will be thrown.
 */
declare(strict_types=1);

namespace 
App\Controllers\Backend;

use 
App\Controllers\BaseController;
use 
App\Controllers\Backend\Authentication\Staff;
//use App\Libraries\Auth;


/**
 * The backend starts here.
 */
class Bootstrap extends BaseController
{

 
/**
 * Let's get it started. But first, who are you?
 */
 
public function index()
 {
 

 
// if ( ! $this->authentication->logged('staff'))
 // { 
 
Staff::login();
 
// }
 // else
 // {
 // $this->dashboard();
 // }
 
}

I thought that once I declare the line
Code:
use App\Controllers\Backend\Authentication\Staff;
I should be able to call any method from that class with
Code:
Staff::method_name();
However, that doesn't work and I'm getting the error:
Code:
Non-static method App\Controllers\Backend\Authentication\Staff::login() should not be called statically
What am I missing?
Thanks


RE: Does the error "should not be called statically" mean my namespace or use is wrong? - kenjis - 01-23-2022

>  Does the error "should not be called statically" mean my namespace or use is wrong?

No.
You need to know the difference between static calls and non static calls.
https://www.php.net/manual/en/language.oop5.basic.php


RE: Does the error "should not be called statically" mean my namespace or use is wrong? - manager - 01-23-2022

Quote:What am I missing?

You are calling non-static method like static. 
Can you show your Staff class?


RE: Does the error "should not be called statically" mean my namespace or use is wrong? - datamweb - 04-07-2022

Look here if you use Laravel : Facades