I'm new to object oriented programming in PHP. I included a class and called it, then, inside this class's constructor I'm calling a private function called handleConnections. For some reason, it's giving me a fatal error (undefined function). Any idea why?
The class:
class Test
{
function __construct()
{
handleConnections();
}
private function handleConnections()
{
//do stuff
}
}
It seems flawless and yet I'm getting this error. If anyone has any clue what might be wrong, please tell me. Thanks!
Just expanding on FWH's Answer.
When you create a class and assign it to a variable, from outside the class you would call any function within that class using $variable->function();. But, because you are inside the class, you don't know what the class is being assigned to, so you have to use the $this-> keyword to access any class properties. General rule of thumb, if you would access it like $obj->var, access it with $this->.
class myClass
{
function myFunc()
{
echo "Hi";
}
function myOtherFunc()
{
$this->myFunc();
}
}
$obj = new myClass;
// You access myFunc() like this outside
$obj->myFunc();
// So Access it with $this-> on the inside
$obj->myOtherFunc();
// Both will echo "Hi"
Try with:
$this->handleConnections();
If you don't prefix your calls with $this, it's trying to call a global function. $this is mandatory in PHP, even when there can be no ambiguity.
Related
I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php'); to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc() like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
In my index.php I'm loading maybe
foobarfunc() like this:
foobar::foobarfunc(); // Wrong, it is not static method
but can also be
$foobar = new foobar; // correct
$foobar->foobarfunc();
You can not invoke the method this way because it is not a static method.
foobar::foobarfunc();
You should instead use:
$foobar->foobarfunc();
If however, you have created a static method something like:
static $foo; // your top variable set as static
public static function foobarfunc() {
return self::$foo;
}
then you can use this:
foobar::foobarfunc();
You are calling a non-static method :
public function foobarfunc() {
return $this->foo();
}
Using a static-call :
foobar::foobarfunc();
When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.
So :
You should not use static calls for non-static methods
Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
Here, the methods of your class are using the current instance of the class, as they need to access the $foo property of the class.
This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
$foobar = new foobar();
$foobar->foobarfunc();
For more informations, don't hesitate to read, in the PHP manual :
The Classes and Objects section
And the Static Keyword page.
Also note that you probably don't need this line in your __construct method :
global $foo;
Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.
To access the $foo class-property, you only need to use $this->foo, like you did.
If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.
If you enable E_STRICT, PHP will raise a Notice about this:
Strict Standards:
Non-static method foobar::foobarfunc() should not be called statically
Do this instead
$fb = new foobar;
echo $fb->foobarfunc();
On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.
First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.
So when you are calling your class function like foobar::foobarfunc(), object is not created.
But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php
Solutions:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
When you call the function in a static context, $this simply doesn't exist.
You would have to use this::xyz() instead.
To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
foobar::foobarfunc();
by :
(new foobar())->foobarfunc();
or :
$Foobar = new foobar();
$Foobar->foobarfunc();
Or make static function to use foobar::.
class foobar {
//...
static function foobarfunc() {
return $this->foo();
}
}
It seems to me to be a bug in PHP.
The error
'Fatal error: Uncaught Error: Using $this when not in object context
in'
appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:
Class_Name
{
function foo()
{
$this->do_something(); // The error appears there.
}
function do_something()
{
///
}
}
While the error is here:
Class_Name::foo();
$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();
Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.
Just use the Class method using this foobar->foobarfunc();
I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php'); to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc() like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
In my index.php I'm loading maybe
foobarfunc() like this:
foobar::foobarfunc(); // Wrong, it is not static method
but can also be
$foobar = new foobar; // correct
$foobar->foobarfunc();
You can not invoke the method this way because it is not a static method.
foobar::foobarfunc();
You should instead use:
$foobar->foobarfunc();
If however, you have created a static method something like:
static $foo; // your top variable set as static
public static function foobarfunc() {
return self::$foo;
}
then you can use this:
foobar::foobarfunc();
You are calling a non-static method :
public function foobarfunc() {
return $this->foo();
}
Using a static-call :
foobar::foobarfunc();
When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.
So :
You should not use static calls for non-static methods
Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
Here, the methods of your class are using the current instance of the class, as they need to access the $foo property of the class.
This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
$foobar = new foobar();
$foobar->foobarfunc();
For more informations, don't hesitate to read, in the PHP manual :
The Classes and Objects section
And the Static Keyword page.
Also note that you probably don't need this line in your __construct method :
global $foo;
Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.
To access the $foo class-property, you only need to use $this->foo, like you did.
If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.
If you enable E_STRICT, PHP will raise a Notice about this:
Strict Standards:
Non-static method foobar::foobarfunc() should not be called statically
Do this instead
$fb = new foobar;
echo $fb->foobarfunc();
On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.
First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.
So when you are calling your class function like foobar::foobarfunc(), object is not created.
But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php
Solutions:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
When you call the function in a static context, $this simply doesn't exist.
You would have to use this::xyz() instead.
To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
foobar::foobarfunc();
by :
(new foobar())->foobarfunc();
or :
$Foobar = new foobar();
$Foobar->foobarfunc();
Or make static function to use foobar::.
class foobar {
//...
static function foobarfunc() {
return $this->foo();
}
}
It seems to me to be a bug in PHP.
The error
'Fatal error: Uncaught Error: Using $this when not in object context
in'
appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:
Class_Name
{
function foo()
{
$this->do_something(); // The error appears there.
}
function do_something()
{
///
}
}
While the error is here:
Class_Name::foo();
$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();
Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.
Just use the Class method using this foobar->foobarfunc();
I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php'); to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc() like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
In my index.php I'm loading maybe
foobarfunc() like this:
foobar::foobarfunc(); // Wrong, it is not static method
but can also be
$foobar = new foobar; // correct
$foobar->foobarfunc();
You can not invoke the method this way because it is not a static method.
foobar::foobarfunc();
You should instead use:
$foobar->foobarfunc();
If however, you have created a static method something like:
static $foo; // your top variable set as static
public static function foobarfunc() {
return self::$foo;
}
then you can use this:
foobar::foobarfunc();
You are calling a non-static method :
public function foobarfunc() {
return $this->foo();
}
Using a static-call :
foobar::foobarfunc();
When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.
So :
You should not use static calls for non-static methods
Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
Here, the methods of your class are using the current instance of the class, as they need to access the $foo property of the class.
This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
$foobar = new foobar();
$foobar->foobarfunc();
For more informations, don't hesitate to read, in the PHP manual :
The Classes and Objects section
And the Static Keyword page.
Also note that you probably don't need this line in your __construct method :
global $foo;
Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.
To access the $foo class-property, you only need to use $this->foo, like you did.
If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.
If you enable E_STRICT, PHP will raise a Notice about this:
Strict Standards:
Non-static method foobar::foobarfunc() should not be called statically
Do this instead
$fb = new foobar;
echo $fb->foobarfunc();
On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.
First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.
So when you are calling your class function like foobar::foobarfunc(), object is not created.
But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php
Solutions:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
When you call the function in a static context, $this simply doesn't exist.
You would have to use this::xyz() instead.
To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
foobar::foobarfunc();
by :
(new foobar())->foobarfunc();
or :
$Foobar = new foobar();
$Foobar->foobarfunc();
Or make static function to use foobar::.
class foobar {
//...
static function foobarfunc() {
return $this->foo();
}
}
It seems to me to be a bug in PHP.
The error
'Fatal error: Uncaught Error: Using $this when not in object context
in'
appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:
Class_Name
{
function foo()
{
$this->do_something(); // The error appears there.
}
function do_something()
{
///
}
}
While the error is here:
Class_Name::foo();
$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();
Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.
Just use the Class method using this foobar->foobarfunc();
I've got a problem:
I'm writing a new WebApp without a Framework.
In my index.php I'm using: require_once('load.php');
And in load.php I'm using require_once('class.php'); to load my class.php.
In my class.php I've got this error:
Fatal error: Using $this when not in object context in class.php on line ... (in this example it would be 11)
An example how my class.php is written:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
In my index.php I'm loading maybe foobarfunc() like this:
foobar::foobarfunc();
but can also be
$foobar = new foobar;
$foobar->foobarfunc();
Why is the error coming?
In my index.php I'm loading maybe
foobarfunc() like this:
foobar::foobarfunc(); // Wrong, it is not static method
but can also be
$foobar = new foobar; // correct
$foobar->foobarfunc();
You can not invoke the method this way because it is not a static method.
foobar::foobarfunc();
You should instead use:
$foobar->foobarfunc();
If however, you have created a static method something like:
static $foo; // your top variable set as static
public static function foobarfunc() {
return self::$foo;
}
then you can use this:
foobar::foobarfunc();
You are calling a non-static method :
public function foobarfunc() {
return $this->foo();
}
Using a static-call :
foobar::foobarfunc();
When using a static-call, the function will be called (even if not declared as static), but, as there is no instance of an object, there is no $this.
So :
You should not use static calls for non-static methods
Your static methods (or statically-called methods) can't use $this, which normally points to the current instance of the class, as there is no class instance when you're using static-calls.
Here, the methods of your class are using the current instance of the class, as they need to access the $foo property of the class.
This means your methods need an instance of the class -- which means they cannot be static.
This means you shouldn't use static calls : you should instanciate the class, and use the object to call the methods, like you did in your last portion of code :
$foobar = new foobar();
$foobar->foobarfunc();
For more informations, don't hesitate to read, in the PHP manual :
The Classes and Objects section
And the Static Keyword page.
Also note that you probably don't need this line in your __construct method :
global $foo;
Using the global keyword will make the $foo variable, declared outside of all functions and classes, visibile from inside that method... And you probably don't have such a $foo variable.
To access the $foo class-property, you only need to use $this->foo, like you did.
If you are invoking foobarfunc with resolution scope operator (::), then you are calling it statically, e.g. on the class level instead of the instance level, thus you are using $this when not in object context. $this does not exist in class context.
If you enable E_STRICT, PHP will raise a Notice about this:
Strict Standards:
Non-static method foobar::foobarfunc() should not be called statically
Do this instead
$fb = new foobar;
echo $fb->foobarfunc();
On a sidenote, I suggest not to use global inside your classes. If you need something from outside inside your class, pass it through the constructor. This is called Dependency Injection and it will make your code much more maintainable and less dependant on outside things.
First you understand one thing, $this inside a class denotes the current object.
That is which is you are created out side of the class to call class function or variable.
So when you are calling your class function like foobar::foobarfunc(), object is not created.
But inside that function you written return $this->foo(). Now here $this is nothing. Thats why its saying Using $this when not in object context in class.php
Solutions:
Create a object and call foobarfunc().
Call foo() using class name inside the foobarfunc().
When you call the function in a static context, $this simply doesn't exist.
You would have to use this::xyz() instead.
To find out what context you're in when a function can be called both statically and in an object instance, a good approach is outlined in this question: How to tell whether I’m static or an object?
Fast method : (new foobar())->foobarfunc();
You need to load your class replace :
foobar::foobarfunc();
by :
(new foobar())->foobarfunc();
or :
$Foobar = new foobar();
$Foobar->foobarfunc();
Or make static function to use foobar::.
class foobar {
//...
static function foobarfunc() {
return $this->foo();
}
}
It seems to me to be a bug in PHP.
The error
'Fatal error: Uncaught Error: Using $this when not in object context
in'
appears in the function using $this, but the error is that the calling function is using non-static function as a static. I.e:
Class_Name
{
function foo()
{
$this->do_something(); // The error appears there.
}
function do_something()
{
///
}
}
While the error is here:
Class_Name::foo();
$foobar = new foobar; put the class foobar in $foobar, not the object. To get the object, you need to add parenthesis: $foobar = new foobar();
Your error is simply that you call a method on a class, so there is no $this since $this only exists in objects.
Just use the Class method using this foobar->foobarfunc();
I am very new to OOP and very rusty on PHP. I was wondering if this is a valid method to call a function from a class?
class newclass {
public function testfunc {
return '1';
}
}
Could I call it like this:
echo testfunc->newclass();
or like this:
echo newclass()::testfunc;
I always see it defined in examples like below and it seemed like extra code?:
$this = new newclass();
$this->testfunc();
echo $this;
Any help would be greatly appreciated as I'm just starting to wrap my head around this OOP thing. If I'm out to lunch maybe someone could suggest a link to a really good guide for a true beginner in classes/OOP. Thanks!
Both ways work and have their use cases.
Your first case is a regular function call using an instance of a class, your second case is a call to a static function.
Static should be used with care and the use of it is very often a sign that refactoring/redesign is necessary.
The point of object oriented programming is to model the world by writing classes (blueprints) and then create as many independent instances of that class with the word new as needed. Each instance is a little organism with the DNA of the class and you can call the same class method on every single instance without influencing the other instances.
A static call however is not related to an instance of a class and therefore there is no object being used. It's a global call of some tool functionality and in fact breaks the idea of encapsulation.
So, I'm not saying there are no use cases for static classes and methods but they should be used with care.
new is the keyword to instantiate the class. If you want to use a method without an instance of the class, it should be a static method. to have a static method, declare the method as static.
class foo
{
public static function bar()
{
return 'hello!';
}
}
How to use it?
echo foo::bar(); //Will print hello
You could make testfunc static and call it like so:
class newclass{
public static function testfunc{
return '1';
}
}
echo newclass::testfunc();
There is nothing like this echo testfunc->newclass(); and doing it like
$class = new newclass();
echo $class->testfunc();
is the proper way to do it when the method is an instance method and not a static one. Note, there is no ability to reference $this within the static method.
You can create a static wrapper for the constructor which would allow for chaining method calls on the same line.
<?php
class ClassA {
static function create() { return new self(); }
function method1() { ... }
}
ClassA::create()->method1();
you can include the php file that contains your functions
<?php
//print.php
function printHello(){
echo "Hello world";
}
?>
then include it and call the function...
<?php
include "print.php";
printHello();
?>