Getting Undefined Property Notice Even Though Its Defined - php

I just started on YII today and have an existing project to work on. When I am trying to run the project, I am getting a following Notice -
Notice: Undefined property: CWebApplication::$v_glob in ..\controllers\SiteController.php on line 10
When I check SiteController class I do see that v_glob is indeed defined. This is how the class looks -
class SiteController extends Controller
{
public function init()
{
Yii::app()->v_glob;
parent::init();
}
/* other functions */
}
Due to this notice I am getting fatal errors wherever I am trying to call its member functions. How can I resolve this?

Well, take a look carefully at the notice :
Undefined property: CWebApplication::$v_glob
The attribute v_glob has to be defined in CWebApplication, not in your SiteController...
PS : And the following line is not really useful:
Yii::app()->v_glob;

Related

Codeigniter: Models not working with an error: Undefined property: CI_Loader

I'm trying out making a simple API with Codeigniter, which is supposed to do basic CRUD tasks on the database related with the form inputs from the main website. I'm mostly a frontend person and don't really have an in-depth knowledge with PHP. It's my first time using the Codeigniter. I'm starting with trying out things on the documentation website, but I ran into a problem working with models.
before actually working with databases, I tried writing a really simple model that just returns "Model_form_receipt" when called. The website gives me an Undefined property: CI_Loader error. This is really troubling because I don't think I even deviated much from the samples from the documentation; perhaps even simpler. Do you have any idea on the cause of this issue and a solution?
The model file (models/Model_form_receipt.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_form_receipt extends CI_Model {
public function test() {
return "Model_form_receipt";
}
}
?>
The view file
<?php
$this->load->model('model_form_receipt');
$data = $this->model_form_receipt->test();
?>
Error Messages
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$model_form_receipt
Filename: form_receipt/base.php
Line Number: 3
An uncaught Exception was encountered
Type: Error
Message: Call to a member function test() on null
Filename: [REDACTED]
Line Number: 3
In Your Controller
**Autoload the model in __construct function.
Thus, your model will run automatically in your function**
It could help
public function __construct(){
parent::__construct();
$this->load->model('Model_form_receipt');
}

Why do I get a smarty notice "Trying to get property of non-object in ...\sysplugins\smarty_internal_templatecompilerbase.php on line 348"?

I create my $smarty object according to the Smarty manual and I get the following notice:
Notice: Trying to get property of non-object in ...\smarty-3.1.32\libs\sysplugins\smarty_internal_templatecompilerbase.php on line 348
What does this mean and how can I avoid it?
This error message appears when you extend the Smarty class but from the new constructor you don't call the parent constructor. Do something like this in your child class:
class SmartyExtend extends Smarty {
function __construct() {
parent::__construct(); // this is the line that was missing
$this->setTemplateDir(...);
$this->setCompileDir(...);
}
}

PHP - accessing a non-class function inside a class

My Try: There is $getSomeData function in a file called bradpitt.php. Its a simple function. Which is not inside a class. Where I have another file name jolie.php. This file is having a class. Where I am trying to access $getSomeData()in that file.
CoolPlugin.php
class CoolPlugin extends plugin
{
const COOLLIST = 'properties/coolBoy.json';
public function getSomeData () {
return DataUtil::readDataFile(self::COOLLIST);
}
bradpitt.php (Non Class File - a simple function)
$getSomeData = function(){
$plugin = new \simulator\CoolPlugin();
return $plugin->getSomeData();
};
jolie.php
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __construct(){
global $getSomeData;
$this->$getSomeData();
}
}
output.php
include_once 'jolie.php';
$joiliePage = new Jolie();
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
How to invoke and access a simple function (having a return as an object) inside another class in PHP?
What I doing wrong where it returns NULL?
The code you posted is full of issues.
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
Assuming is line 173 is the one listed above, both error messages tell the same thing: the variable $joiliePage was not initialized (and the interpreter considers its value is NULL).
Don't get fooled by the fact that PHP classifies them as "Notices". They are notices from the interpreter's point of view (it cannot find a variable) but they are errors for your code as it cannot continue successfully.
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __contruct(){
global $getSomeData;
$this->$getSomeData()
}
}
The function is called __contruct() but you probably want it to be the class' constructor. It is not the constructor and it is not called automatically by the interpreter because it doesn't have the correct name. The name of the constructor is __construct(). Notice there is an "s" in the middle that is missing in your code.
The method __contruct() declares the global variable $getSomeData. If the file bradpitt.php is successfully included (it may fail with a warning without breaking the script if the file does not exists) then the $getSomeData symbol refers to the variable with the same name defined in file bradpitt.php.
However, the call $this->$getSomeData() doesn't refer to this global variable. It uses the class' property with the same name, which is initialized. This code won't run.
In order to call the function stored in the global variable $getSomeData, the code should read:
public function __construct(){
global $getSomeData;
$getSomeData();
}
Also notice that the statement is missing a semicolon at the end and produces a syntax error. Your class' definition is incorrect, it doesn't compile and objects of type Jolie cannot be created.

Reason for Undefined class constant NOTICE in PHP (Use of undefined constant)

I have discovered a weird problem in my code regarding class constants. While it seems that the code does work correctly, I cannot figure out the reason of PHP Notice I am getting:
Use of undefined constant PAYMENT_ERROR - assumed 'PAYMENT_ERROR' in /src/Micro/Payments/Manager.php on line 146
The code in Manager.php function looks like this:
$code = Result::PAYMENT_ERROR;
return new Result($code, $errMsg); // <- line 146 - causes PHP Notice
What is strange to me, is that $code variable is set correctly and does not trigger any notices. Only instantiating Result does.
The Result class is very simple:
class Result
{
// ... boilerplate code skipped ...
// constant is defined like this:
const PAYMENT_ERROR = 2;
public function __construct($code, array $messages)
{
$this->code = $code;
$this->messages = $messages;
}
// ... other functions skipped as they are not relevat ...
}
Is there a problem that I pass Result's constant to it's own constructor?
I have found the reason for this notice and fixed it.
I have had this line in Result class:
protected $code = PAYMENT_ERROR;
This was causing the notice above, as I did not define this correctly. I would have expected PHP to tell me where the error message was coming from exactly, when instantiating new Class, instead of just pointing to a line where said Class is instaniated.
So the fix was to change it to this:
protected $code = self::PAYMENT_ERROR;
See the difference define() vs const
You must be using the PAYMENT_ERROR outside the class.
If you want to do so use the define().
This will do the job.

PHP Class; Notice: Undefined property error

before I ask my question, I would like to say that I searched for this question, and none of the other answers helped...
Basically, in my class DemoClass, I have 4 functions, and all of them are "undefined properties"
My error:
Notice: Undefined property: DemoClass::$function in /home/content/92/10270192/html/class.php on line 46
Note: line 46 is where i do $demoClass->function...
I have a typical class setup:
class DemoClass {
public function __construct () {
// stuff that works and gets called
}
public function testFunct () {
// one that is an "undefined property"
}
}
I access the class as normal:
$testClass = new DemoClass();
var_dump(testClass->testFunct); // this is what is on line 46
// ^^^ This also gives me NULL, because its undefined (? i guess...)
I've never had this problem before, any suggestions? Thanks!
Brackets are required when calling a function. Change it to $testClass->testFunct() instead.
$testClass->testFunct references a variable testFunct in the class. You need to use $testClass->testFunct() to reference a function in the class.
It should be
var_dump(testClass->testFunct())
A function always needs the parentheses as else (as you can see) you can't tell the difference between a function and a constant.
Unlike for instance JavaScript, PHP is not handling class methods as regular properties.
When you use $testClass->testFunct, PHP looks for a property named testFunct and finds none.
Methods can be referenced through class name, DemoClass::testFunct in your case.

Categories