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.
Related
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.
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.
I wrote three methods in a class and one is calling another, but when I call the function outside through the object, it is showing an undefined function error for the second function.
Here's my code:
function resize_image(){
}
function image_resize(){
$a = resize_image();
}
When I run this, it shows resize_image() as undefined. Here's the error:
Fatal error: Call to undefined function resize_image() in
/home/vacayge/public_html/Major/Alpha1/classes/cUserImages.php on line
2090
Using this you can access the function inside the class
put this code
$a = $this->resize_image();
The thing is that when you call a function that belongs to an object you need to specify which object it belongs to. Similarly if you access a variable that belongs to an object then you need to specify which object it belongs to.
Inside Object
$this->my_func();
$this->my_var = 'foo';
outside Object
$my_obj->my_func();
$my_obj->my_var = 'foo';
Static Stuff(not required by your question but added for completeness)
MyClass::my_func();
MyClass::my_var = 'foo';
$this->resize_image(); this is the way to call a function from outside that function
http://query7.com/using-this-in-php check out for more details
I've read several of the threads that already exist with this, or a similar, name. None seems to address my situation exactly.
I'm getting this error from my utils.php class.
Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89
I define the variable debug like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
and then call it (in line 89) like this:
$debug->debug($message);
The reason I'm so baffled is that I copied and pasted these lines from my index.php and that call works just fine.
If you want, I can include links to the index.php and utils.php files, as well as PHPDebug.php.
Thanks to your last comment, the solution to your problem is to declare $debug as global within the function. Thus, you should have something like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
function myfunction()
{
global $debug;
// some code
$debug->debug($message);
}
You can read more about global in the official doc.
I have a header method that shows in the top of a page, it is inside a class, inside my header() method I run this code here to start a new Profiler object...
//start new page timer object
$profiler = new Profiler;
$profiler->start();
After a bunch of other files are compiled, I then include a file into the footer section, in this file I run this code,
echo 'Page Generated in ' .$profiler->end(). ' of a second with ' .$_SESSION['querie_counter']. ' MySQL Queries';
However I am getting this error message in the footer file now,
Notice: Undefined variable: profiler
in
C:\webserver\htdocs\friendproject2\includes\footer.inc.php
on line 21
Fatal error: Call to a member function
end() on a non-object in
C:\webserver\htdocs\friendproject2\includes\footer.inc.php
on line 21
How can I fix this?
If you created the $profiler object inside the header method, it will not be available in another method, unless it was a global $profiler was a global variable or it was a singleton.
To make it global, declare $profiler outside the header method and then inside the header method, include this line:
global $profiler;
Include this line in the the footer method as well. The rest of your code can stay the way it is. It should work.
Variables created in a function are local to that function. Use the global keyword to declare a global variable.
I'd suggest make the $profiler a property of the main class and initialize it inside the constructor method, since it's not really related to the header.
Assuming this is the main class
class Example {
private $profiler;
public function __construct() {
$this->profiler = new Profiler;
}
public function header() {
...
}
}
Inside the included header, initialize this object and run header, and just to make it more explicit, make the profiler start call separately:
$example = new Example();
$example->profiler->start();
$example->header();
Inside the included footer:
$example->profiler->end();
Possible Steps:
First of all make sure that you include the file which contains the profiler class.
If you are using the profiler instance inside a function, you need to use the global keyword:
global $profiler;
I Think if you remove session() from the top of your header your problems will be fixed ...