0

I was trying a tutorial from another place to show my data from an instance object. But when i try to implement it, i found error Message: Class 'Booking' not found Filename: C:\xampp\htdocs\teja\application\controllers\controlBooking.php

this is my code in class Booking

<?php namespace application\classes;
class Booking {
    protected $id;
    protected $name;
    protected $address;
public function setId(string $id){
    $this->id = $id;
}
public function getId(){
    return $this->id;
}
public function get_booking()
{
    $query = $this->db->select("*")
             ->from('booking')
             ->order_by('id', 'DESC')
             ->get();
    return $query->result();
}...

And this is my model class

<?php use application\classes\Booking;
class modelBooking extends CI_Model{
    public function get_all(){
        $book = new Booking();
        $testimoni->get_booking();
    }
}

and this is my controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class controlBooking extends CI_Controller {
public function __construct(){
    parent ::__construct();
    //load model
    $this->load->model('modelBooking'); 
}
public function index(){
    $data = new Booking();
    $this->load->view('viewBooking', $data);
}

then how i show it in my view class? how to fix that error? thankyou for all of you, please help me to solve this

  • Does your application use any sort of autoloading? For CodeIgniter see [this guide](https://www.codeigniter.com/user_guide/general/autoloader.html). – Jirka Hrazdil Apr 14 '19 at 11:32
  • @JiriHrazdil when i add $autoload['libraries'] = array('database', 'session', 'classes'); still found an error: Unable to load the requested class: Classes. how do i add an autoloading for my class? – Laquinta Id Apr 14 '19 at 11:38
  • In the model what is the object `$testimoni`? It is not defined anywhere we can see. Also, what do you do with `$book` created with `$book = new Booking();`? – DFriend Apr 14 '19 at 15:06
  • @DFriend sorry it was my typo, i mean `$book->get_booking();` after i created `$book = new Booking();` – Laquinta Id Apr 15 '19 at 00:24

2 Answers2

1

First, the file Booking.php should be moved to the folder /application/libraries/

Then, instead of using new, use the standard CodeIgniter library loading scheme, e.g.

$this->load->library('booking');

The CodeIgniter 'Loader' (ie. $this->load->...) will locate the class file (if it is in the right place), include it, and instantiate the named class.

Wherever you want to use the instance created by the above use

$this->booking->some_method();
DFriend
  • 8,869
  • 1
  • 13
  • 26
0

CodeIgniter won't autoload namespaced classes out of the box (that I'm aware of), and isn't designed to be used like you would in Laravel or Symphony (entities don't really exist). I highly doubt the tutorial you followed was CI specific.

This will change in CI 4 which is currently in beta testing.

For now, I would suggest either making it a library as DFriend suggested and loosing all the use and namespace declarations and just loading it the CI way or you can follow along with my answer here: How do I include the OOP "defiant randomdotorg" library in codeigniter?

Please note: whether you are using a library or the above method you cannot access $this->db or anything CI-related directly as you would in a model or controller. You'd have to do:

$CI = &get_instance;
$CI->db->get(...);

obviously the get instance part can be done in the class constructor and assigned to a variable for usage in multiple functions.

Alex
  • 9,215
  • 8
  • 39
  • 82
  • thankyou verymuch! now i can get my code running but, if i want to put my data from database in settter getter method like: `$this->booking->setName(take from database);` how can i do that?? – Laquinta Id Apr 15 '19 at 03:24
  • im not sure what you want to do, make a new question. – Alex Apr 15 '19 at 09:29