I have view class like this:
class View {
public function __construct() {
}
public static function render($name) {
require 'views/user/header.php';
require 'views/user/'.$name.'.php';
require 'views/user/footer.php';
}
}
and I call the view class in controller like this:
class Controller {
function __construct() {
$this->view = new View();
}
}
and then I set the view property from controller child class, like this:
class Index extends Controller {
function __construct() {
parent::__construct();
$this->view->js = "test";
}
public function index() {
$this->view->render('index/index');
}
}
But when I want to get $this->js from "header.php" which is set at render function on view class, I always get this error message:
Fatal error: Uncaught Error: Using $this when not in object context
I was tried to check, Am I in the right class? using this methods in "header.php" file:
echo get_class(); // and this method return "View";
that means I was on the view class, right?
Can anyone please help me?
Thanks in advance
You have defined render() as a static method, but you are calling it as it was not static.
I would probably benefit from reading this: http://chadminick.com/articles/simple-php-template-engine.html
P.S. What you call "view" is just a template.
Related
I created a class named function and calling function method in another class named test. But it shows me an error -
PHP Fatal error: Call to a member function
my function class's object name is $obj = new functions();
when i call user method which is written in function class from test class I am getting an error.
class className {
function users(){
$users = "Demo User"; return $user;
}
}
$obj = new functions();
you mean this:
you can extends
class className extends test{
function __construct(){
here you add
}
}
Im trying to pass objects between classes in code igniter and am currently failing. What am I doing wrong. Let me strt showing the pure.php version
Errors.php
<?php
class Errors
{
public function __construct(){}
public function setError($msg){}
}
OtherClass.php
<?php
class OtherClass
{
public function __construct(Errors $errorObject) {}
public function someMethod() {}
}
Then in my main controller..
Controller.php
<?php
class Main
{
public function __construct()
{
$this->errors = new Errors;
$this->other = new OtherClass($this->errors);
}
}
By doing this. I can add errors as I go to my error Object, across any objects i instantiate from the Main controller.
Now my code igniter version looks like this
/library/Errors.php
<?php
class Errors
{
public function __construct(){}
public function setError($msg){}
}
/library/OtherClass.php
<?php
class OtherClass
{
public function __construct(Errors $errorObject) {}
public function someMethod() {}
}
Then in my main controller..
Controller.php
<?php
class Main extends CI_Controller
{
public function __construct()
{
$this->load->library('Errors');
$this->load->library('OtherClass',$this->errors);
}
}
When I do this I get an error in my OtherClass saying that $errorObject is not an instance of Errors. Why is the object not being passed?
The problem is with $this->load->library which is defined like this.
public function library($library, $params = NULL, $object_name = NULL)
$params is expected to be an array. If it is not then the $params is set to NULL.
To get around this requires a bunch of monkey biz.
class Errors is unchanged but class OtherClass needs to be changed to...
class OtherClass
{
public function __construct($errorObject)
{
var_dump($errorObject[0]); //so we can prove it got passed
}
public function someMethod(){}
}
Note the removal of the type hint Error from the constructor declaration. Also, we access index 0 of the argument. The reason lies in what happens at the controller.
function __construct()
{
parent::__construct();
$this->load->library('Errors');
$this->load->library('OtherClass', [$this->errors]);
}
We have to put $this->error in an array so that load->library() won't mess with it.
The alternative is to not use "The CodeIgniter Way" and use good old fashion `new' instead. The controller then is...
function __construct()
{
parent::__construct();
$this->load->library('Errors');
$this->other = new OtherClass($this->errors);
}
And other class reverts to...
class OtherClass
{
public function __construct(Errors $errorObject)
{
var_dump($errorObject);
}
public function someMethod(){}
}
Now the problem is that without adding an autoloader to the system you wind up with
Fatal error: Class 'OtherClass' not found in ...
This LINK goes to a page that talks about the various ways to add an autoloader to CI. I know this has been answered on SO too. But I'm failing to find it at the moment.
Cant seem to wrap my head around $this!
I am trying to emulate a codeigniter function of loading views but i am obviously missing something.
class Load{
public function __construct(){
}
public function view(){
echo "Hello";
}
public function files(){
}
public function plugins(){
}
}
$this->load->view();
This throws the following
Fatal error: Using $this when not in object context
However when i use:
$load = new Load;
$load->view();
I get the expected response. Hello
Why does it work in Codeigniter but not in my simple script?
Ive already googled and searched SO..
In your code you use $this not in a class. So it is really not an object.
You should create the object of class before use it.
Or if you want to use it in other class, just do something like that:
class PreLoad
{
public load;
public function __construct()
{
$this->load = new Load();
}
public function show()
{
$this->load->view();
}
}
(new PreLoad())->show();
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
I'm working on a small MVC framework in PHP for an exercise. PHP, however, doesn't seem to like my Controller class. The class contains an instance of a loader that loads views:
abstract class Controller
{
public $load;
function __construct($load)
{
$this->load = $load;
}
abstract public function index();
}
From there, I can override Controller for all my controllers. For instace, my index controller:
class Index extends Controller
{
public function index()
{
$this->load->view("hello_world");
}
}
But when I create it:
require 'Controller.php';
require 'Load.php'
require 'controllers/Index.php';
$i = new Index(new Load());
$i->index();
I get this error:
PHP Fatal error: Call to a member function view() on a non-object in /var/www/controllers/Index.php on line 7
Can you guys help me out? I know I set the load in the constructor, and the load class does have a method called view, so why is it giving me this error?
Also: Load class, just for good measure
class Load
{
public function view($filename, $data = null)
{
if(is_array($data)) extract($data);
include ROOT.DS.'views'.DS.$filename.'.php';
}
}
The problem is with this code, and it's not always obvious:
class Index extends Controller
^^^^^
{
public function index()
^^^^^
{
$this->load->view("hello_world");
}
}
This is the same name and therefore a PHP 4 backwards compatible constructor. The parent's constructor then is not called, $load not set and the function not defined.
Knowing this, there are many solutions, including:
namespace DelishusCake;
Introduce a Namespace
This automatically fixes your issue. You need to place this on top of the file.
class Index extends Controller
{
public function index($load = NULL)
{
isset($load) && $this->load = $load;
$this->load->view("hello_world");
}
}
Make the PHP4 backwards compatible constructor work
Or:
class MyIndex extends Controller
{
public function index()
{
$this->load->view("hello_world");
}
}
Rename the class
Or:
class Index extends Controller
{
public function __construct($load) {
parent::__construct($load);
}
public function index()
{
$this->load->view("hello_world");
}
}
Add a PHP 5 constructor, call the parent's constructor
Keep in mind that you only need this because it's the same name. The in depth description you can find as well in the PHP Manual on the Constructors and Destructors page.
You need to instantiate the parent class.
class Index extends Controller
{
public function __construct($load) {
parent::__construct($load);
}
public function index() {
$this->load->view("hello_world");
}
}
I'm trying to build MVC pattern using OOP PHP. Please read the rest of the post to understand what I want exactly.
This is the homepage controller which extends the main controller
class Home extends Controller {
function __construct () {
parent::__construct();
}
public function index () {
$this->load->model("test");
$this->test->get_all();
$data = array (
'name' => "Amr",
'age' =>24
);
$this->load->view("home_view",$data);
}
}
The Main Controller looks like this and extends loader class:
class Controller extends Loader {
public $load;
function __construct(){
parent::__construct();
$this->load = new Loader();
}
}
The Loader class which has the problem looks like this
class Loader {
public $Error;
function __construct(){
$this->Error = new Error();
}
public function model($Modelname){
$this->$Modelname = $Modelname;
if (file_exists("models/".$Modelname.".php")){
require_once $Modelname . ".php";
$this->$Modelname = new $Modelname;
}else{
$this->Error->Not_found();
}
}
public function view($Viewname,$data=NULL){
if(is_array($data)){
extract($data);
}
if (file_exists("views/".$Viewname.".php")){
require_once $Viewname . ".php";
}else{
$this->Error->Not_found();
}
}
public function helper($helper) {
if (file_exists("helpers/".$helper.".php")){
require_once $helper . ".php";
$this->$helper = new $helper;
}else{
$this->Error->Not_found();
}
}
}
What I need to do is to be able FROM HOMEPAGE Controller to do something like this:
$this->load->model("someModel"); // model name is test
$this->someModel->someMethodInModel(); // the model method is get_all()
// and the same for helper
$this->load->helper("someHelper");
$this->someHelper->someMethodInHelper();
Can anyone help me?
EDIT: The error that I'm getting when doing this is:
Notice: Undefined property: Home::$test
Fatal error: Call to a member function get_all() on a non-object
NOTE: the model name is test and the model method is get_all()
public function model($Modelname){
$this->$Modelname = $Modelname;
if (file_exists("models/".$Modelname.".php")){
require_once $Modelname . ".php";
$this->$Modelname = new $Modelname;
What do you need help with? With your current code you can do
$this->load->model("someModel");
$this->someModel->someMethodInModel();
// and the same for helper
$this->load->helper("someHelper");
$this->someHelper->someMethodInHelper();
in your Home controller. I just tried it out myself. What errors do you get? What's not working properly?
If I were you, I would put a "load()" method into my controller which would call the "loader" class and store the new model/helper/view into a variable.
I think the problem you're having is, your loader creates an instance of a model as a member of the Loader class, and not your controller class.
if your base controller extends Loader then there is no reason use it like property
class Controller extends Loader {
public $load;
function __construct(){
parent::__construct();
$this->load = new Loader();
}
}
change it to
class Controller extends Loader {
}
another way is to use magic method hook http://php.net/manual/en/language.oop5.overloading.php#object.get
class Controller {
public $load;
function __construct(){
parent::__construct();
$this->load = new Loader();
}
function __get($modelName){
return $this->load->$modelName;
}
}