Undefined property when extending CI_Model in CodeIgniter [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Codeigniter Message: Undefined property: stdClass
location: application/core/student_model.php
class Student_model extends CI_Model
{
private $id;
public function __construct() {
parent::__construct();
}
public function setId($id){
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
location: application/controllers/test.php
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Student_model');
}
public function index()
{
$student = $this->student_model->setId('1234567');
echo $student->getId();
}
}
I am getting the following message/error.
Message: Undefined property: Test::$student_model
Filename: controllers/test.php
Line Number: 13
This line is where I am calling the method setId.
Can anyone see what I am doing wrong?

Try replacing
$this->student_model->setId('1234567');
with
$this->Student_model->setId('1234567');
Your class is capitalized, so the property should also be capitalized afaik.

Try
public function __construct()
{
parent::__construct();
$this->load->model('Student_model', 's_model');
}
public function index()
{
$student = $this->s_model->setId('1234567');
echo $student->getId();
}

what you are doing wrong is :
you are assigning $student to $this->student_model->setId('1234567');
which is a function or 'setter' that does not return anything; and you won't be able to use $student->getId();
what I would do is that
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('student_model', 'student');
}
public function index()
{
$this->student->setId('1234567');
echo $this->student->getId();
}
}

Related

Call to a member function generate() on null

I am getting this error
"Fatal error: Uncaught Error: Call to a member function generate() on null in /var/www/example.loc/app/Controllers/ControllerMain.php on line 17"
when calling the action_index function;
This is the ControllerMain.php file; general_view.php is my layout;
<?php
namespace App\Controllers;
use App\Models\Users;
use Core\Controller;
class ControllerMain extends Controller
{
public function __construct()
{
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}
File Core\Controller
<?php
namespace Core;
class Controller {
public $model;
public $view;
public function __construct()
{
$this->view = new View();
}
public function action_index()
{
}
}
File Core/view.php
<?php
namespace Core;
class View
{
//public $template_view; // здесь можно указать общий вид по умолчанию.
function generate($general_view, $data = null)
{
if(is_array($data)) {
// преобразуем элементы массива в переменные
extract($data);
}
include 'app/views/'.$general_view;
}
}
I could not find the right solutions for myself, so I decided to write here, so I hope for your help
I ask you to help solve the problem, because I am new to php, and sorry for my bad english :)
Perhaps I did not give all the information to understand what was going on, so if you need something, write to me
Please correct me if I'm wrong, I guess you need parent::__construct(); in ControllerMain.
Example:
class ControllerMain extends Controller
{
public function __construct()
{
parent::__construct();
$this->model = new Users();
}
public function action_index()
{
$this->view->generate('general_view.php');
}

A PHP Error was encountered Message: Undefined variable: menu - CodeIgniter

<?php
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->$menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->$menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Hi guys, I get menu details at the constructor like this.I can get the result also. but it comes with the error
"A PHP Error was encountered
Severity: Notice
Message: Undefined variable: menu
"
You need to remove the dollar sign before menu, because you're referencing local variables
$this->menu = $this->menu_model->generateTree($items=array());
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Use $menu as below in your file, hope this solution help you.
$this->menu
Remove $ from $this->$menu
public function __construct()
{
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items=array());
}
public function index()
{
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
while accessing the class variable you need not to use $ for second time after $this
so it would be like below code
for example
$this->variable_name
your code would be as follows
<?php
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}

function return to a variable in utility.php in codeigniter

I am new in codeigniter.
I need to do the following code in utility.php which is a custom library.
class Utility{
public $x=test("123");
public function test($name)
{
return $name;
}
echo $x;
}
Thanks in advance
This is showing syntax error.
Maybe this way?
class Utility
{
public $x;
public function __construct($name = 'default name')
{
$this->x = $this->test($name);
}
public function test($name)
{
return $name;
}
}
Then in controller you would go with
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index($name = null)
{
$this->load->library('utility', [$name]);
echo $this->utility->x;
}
}

Call to a member function get on a non-object

I have this class:
class Search
{
protected static $Basics;
public function __construct() {
self::$Basics = new Basics();
}
public static function getT() {
return self::$Basics->get('keywords/t');
}
public static function isAvailable($keyword) {
return self::$Basics->get('keywords/available', ['keyword' => $keyword])['available'];
}
}
The class Basics is really simple class:
class Basics
{
public function __construct()
{
//some code..
}
public function get($keyword, $param = null)
{
return ['available' => true];
}
}
Call to getT function:
use App\Libraries\Search;
class GV
{
public function test() {
echo Search::getT() ? 'ok' : 'bad';
}
}
But, when i run the function getT in class Search, it return this error: Call to a member function get() on a non-object
What can i do?
You are calling the method inside Search statically (Search::getT();) which will never fire the __construct() method.
__construct() gets fired upon instantiating the class ($search = new Search;), not upon calling static methods (Class::method();).
Simply instantiate your search object: $search = new Search;
Like so:
use App\Libraries\Search;
class GV
{
public function test() {
$search = new Search;
echo $search::getT() ? 'ok' : 'bad';
}
}

Why can I not pass values initialized in the constructor of my Code Igniter controller?

I have this controller in Code Igniter application. A value is initialized in the constructor.
class Cat extends CI_Controller {
private $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $data);
}
}
The view "views/myCatPage.php" looks like this. It is simple.
<?= $sound ?>
Why does PHP note this error?
Message: Undefined variable: sound
I thought I sent this variable as a key in the array ($data) I sent into the view.
I have tried
$this->load->view('myCatPage', $this->data);
but that strangely fails too.
class Cat extends CI_Controller {
var $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $this->data);
}
}

Categories