• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create a new object CI4

#1
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
Reply

#2
Add protected $model then you can call with $this->model
Hi! I am Jamil
Reply

#3
(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
Reply

#4
Resolved:

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

#5
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
Reply

#6
(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
Reply

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

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

// or

use \stdClass 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.