CodeIgniter Forums
create a new object CI4 - 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: create a new object CI4 (/showthread.php?tid=75465)



create a new object CI4 - gra - 02-08-2020

Goodmorning everyone,
i am using codeigniter version 4 rc3.
I would like to create a model object for CI3 compatibility.

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\MO_Home;

class 
Home_1 extends BaseController
{
    
    
function __construct(){
        
        $model
= new stdObject();
        $this->model->MO_Home = new MO_Home(); 

usage
PHP Code:
public function index()
 {
    echo $this->model->MO_Home->call_function(); 

It gives me error in creating the object.

Thank you


RE: create a new object CI4 - mjamilasfihani - 02-08-2020

Add protected $model then you can call with $this->model


RE: create a new object CI4 - gra - 02-08-2020

(02-08-2020, 08:23 AM)mjamilasfihani Wrote: Add protected $model then you can call with $this->model
the error is:
Class 'App\Controllers\stdObject' not found


RE: create a new object CI4 - gra - 02-08-2020

Resolved:

PHP Code:
$this->model= (object) null



RE: create a new object CI4 - jreklund - 02-09-2020

Hi, in PHP the actual name for an empty class are this.
PHP Code:
$this->model = new stdClass(); 

Don't forget to add this in your BaseConroller. And initialize it there, so that you can use it all controllers.
PHP Code:
protected $model



RE: create a new object CI4 - gra - 02-10-2020

(02-09-2020, 03:05 AM)jreklund Wrote: Hi, in PHP the actual name for an empty class are this.
PHP Code:
$this->model = new stdClass(); 

The problem is in php 7.4
PHP Code:
new stdClass(); 


the error is:

Class 'App\Controllers\stdObject' not found


I read that for php write:
(object) Null;
or
new stdClass ();
it's the same thing.

But I don't understand why he can't find the class.
i don't know if i have to add use something \\ other \\ object functions

Thanks


RE: create a new object CI4 - InsiteFX - 02-11-2020

Try one of these and see if it will work for you.

PHP Code:
$this->model = new \stdClass();

// or

use \stdClass