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.
Related
I am sure I've seen once example of assigning $this to another variable, single-letter to make the code typing quicker.
class Dclass {
private $d = $this;
}
But it's not working, is throwing me an error :
Fatal error: Constant expression contains invalid operations in ..
Okay, so from the another post How to handle class variable without $this?
"$this is only available inside functions within the class." #FrankerZ
So this kind of assignment should be done inside the class constructor, or other function?
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.
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.
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;
I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();