• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can't create instance of a model class in models from controller class

#5
(11-12-2019, 09:07 AM)captain-sensible Wrote: @InsiteFx cheers for the heads  up


in my controller Home.php(in Controller dir ) i have

PHP Code:
<?php namespace App\Controllers;
 use 
App\Models\NewsModel;
 

class 
Home extends BaseController
{
    public function index()
   
    
{
       
     $model 
= new NewsModel();   

    then in my model NewsModel.php (in Models dir) I have
PHP Code:
<?php namespace  App\Models


class 
NewsModel 


{
       public function index()
       
       
{
 
   
    
    



i don't want to extend any class , connect to a databse or anything else ,just get system to find class

Now if i understand Namespace & use correctly because i stated full namespace in controller

PHP Code:
use App\Models\NewsModel;

then i should be able to use short code 
PHP Code:
$model = new NewsModel();   

i just dont get it 
i wonder if its some sort of autoload issue or something else in system 

I don't get it either because if I recreate what is quoted above it works perfectly.

For your reference here's what I did.

APPPATH/Controllers/Home.php

PHP Code:
<?php namespace App\Controllers;

use 
App\Models\NewsModel;

class 
Home extends BaseController
{
 public function 
index()
 {
 
$model = new NewsModel();
 echo 
$model->getSome('Hello World');
 }



APPPATH/Models/newsModel.php

PHP Code:
<?php namespace App\Models;

class 
NewsModel
{
 public function 
getSome($param)
 {
 return 
$param;
 }


FWIW, I don't see any issue with your Apache site config.

Have you made any changes to /Config/Autoload.php ?
Reply


Messages In This Thread
RE: can't create instance of a model class in models from controller class - by dave friend - 11-12-2019, 01:41 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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