I am really going crazy with this problem.
I am not able to call a method from the parent class in a static method of a child class..
This is what I tried but it does not work..
class custom extends service {
private $service;
function __construct() {
parent::__construct();
$this->service = new service;
}
public static function activematches($callback) {
$select_by_user = parent::$db->select('matches', '*', array('user_id' => $user_id,
if (count($select_by_user) == 0 && count($select_by_opponent) == 0)
parent::$check->send('11');
else
$this->service->make($callback['request'], $callback['data']);
}
When I call $this->service I get:
Fatal error: Using $this when not in object context
I tried making that as static, I tried putting the same method in the child class by calling the parent method parent::method, but nothing...
I am new to OOP, any help?
For access within a static invocation the property must be defined static as well
protected static $services;
From there you need to reference within your static methos as either.
self::$services
or
static::$services
Referencing self in this context will refer to $services property where the reference is defined. static will reference the property from the class context that the reference was invoked on. For more information see what the manual has to say about late static binding
UPDATE
Based on the fact that custom extends service in this case I doubt this is what you are really after. A class definition like :
class custom extends service {
public function activematches($callback, $user_id) {
$select_by_user = $this->db->select('matches', '*', array('user_id' => $user_id));
if (count($select_by_user) == 0 && count($select_by_opponent) == 0)
$this->check->send('11');
else
$this->make($callback['request'], $callback['data']);
}
}
May be closer to what you want.
If the parent method, make is not static:
You can't call a non-static method from the parent class from a static method of the child class. Have you considered making the child method not-static? I think that's your best option here.
If the parent method make is static:
parent::make($callback['request'], $callback['data']);
But this is called Late Static Bindings which was introduced in PHP 5.3.0. It won't work in older versions, so be careful with that.
Fatal error: Using $this when not in object context
This is actually answer for your question. The specific of static members of class is - you can use them without making an object. $this - reference for object, in which context method was called.
So try to look on problem in this way - in static member you don't have any $this.
You can only use static members of parent class in this way - self::method.
Or, you can actually create an object on parent class and use any "dynamic" method in "dynamic" notation, but this will make you even more crazy later, believe me)
Related
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
PHP > The Basics
I just started learning OOPS in php. I wrote a simple program for implementing inheritance. I am getting a fatal error of $this when not in object context. Can anyone explain me this error, what does it mean?
here is my code:
<?php
class human{
public $gender;
public function _construct($gender)
{
$this->gender=$gender;
echo $this->get_gender();
}
public function get_gender()
{
return $this->gender;
}
}
class person extends human{
public $name;
public $surname;
public static function set_name($name)
{
$this->name=$name;
}
public static function set_surname($surname)
{
$this->surname=$surname;
}
public static function get_name()
{
return $this->name;
}
public static function get_surname()
{
return $this->surname;
}
}
$john = new person('male');
$john->set_name('John');
$john->set_surname('Williams');
echo $john->get_name().' '.$john->get_surname().'is a '.$john->get_gender();
?>
There are two problems here:
You have defined your methods as static. You should not do that as they are not, they depend on being called on an object as you want to use the objects non-static properties.
You have a typo in your constructor function. The correct name for the constructor is __construct, notice the two _ at the beginning.
In a static function, $this does not exist. You can refer to the class itself with self::. So, instead of $this->surname, use self::surname. As noted below, this will change your error to a new error as it requires the variables to be declared static as well.
Of course, you really need to figure out WHY you made those functions static. Should they be static?
You're trying to access the $this property from a static method. You cannot do this, as anything static can be accessed without instantiating the class, therefore the $this variable would not make sense, as you are not in an actual class instance.
The this keyword refers to the current object you are working with, e.g. if you were to make 5 instances of person, each $this would refer to that specific instance of person, and you could change each objects properties freely.
When you use static you are not referring to any instance, you are simply accessing static variables and calling static methods of that class. All these variables and methods are 'class wide'. That means if you change the static property in one instance of the class, the static property will change in all the classes.
Another way to imagine this is, take when calling the new keyword, you create a new instance of the object. Every instance will have its own copy of all the properties and methods you describe in the class, EXCEPT for the static ones. Imagine that when you access a static method or a static property you are accessing one object always.
That's why it would not make sense to access $this from a static context. You are not referring to any specific instance, only the class itself.
I hope I've explained this well enough, it's a very important concept to grasp in OOP, and it does give some people a lot of trouble to understand the concept.
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
PHP > The Basics
I have this method that I want to use $this in but all I get is: Fatal error: Using $this when not in object context.
How can I get this to work?
public static function userNameAvailibility()
{
$result = $this->getsomthin();
}
This is the correct way
public static function userNameAvailibility()
{
$result = self::getsomthin();
}
Use self:: instead of $this-> for static methods.
See: PHP Static Methods Tutorial for more info :)
You can't use $this inside a static function, because static functions are independent of any instantiated object.
Try making the function not static.
Edit:
By definition, static methods can be called without any instantiated object, and thus there is no meaningful use of $this inside a static method.
Only static functions can be called within the static function using self:: if your class contains non static function which you want to use then you can declare the instance of the same class and use it.
<?php
class some_class{
function nonStatic() {
//..... Some code ....
}
Static function isStatic(){
$someClassObject = new some_class;
$someClassObject->nonStatic();
}
}
?>
The accessor this refers to the current instance of the class. As static methods does not run off the instance, using this is barred. So one need to call the method directly here. The static method can not access anything in the scope of the instance, but access everything in the class scope outside instance scope.
It's a pity PHP doesn't show a descriptive enough error. You can not use $this-> inside a static function, but rather use self:: if you have to call a function inside the same class
In the static method,properties are for the class, not the object.
This is why access to static methods or features is possible without creating an object.
$this refers to an object made of a class, but $self only refers to the same class.
Here is an example of what happens when a method of a class is called in a wrong way. You will see some warnings when execute this code but it will work and will print: "I'm A: printing B property". (Executed in php5.6)
class A {
public function aMethod() {
echo "I'm A: ";
echo "printing " . $this->property;
}
}
class B {
public $property = "B property";
public function bMethod() {
A::aMethod();
}
}
$b = new B();
$b->bMethod();
It seams that the variable $this, used in a method which is called as a static method, points to the instance of the "caller" class. In the example above there is $this->property used in the A class which points to a property of the B.
EDIT:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
PHP > The Basics
i was wondering why there is no $parent->function(); syntax in php, but instead we can use parent::function(); which looks like it's used inside a static class. Am i missing some php oop basics?
I admit it seems strange -- and you didn't miss anything in the manual ^^
But :
Generally, when the child class re-defines a method that's already defined in the parent class, you want the child's method to totally override the parent's one
except for __construct, I admit -- that's probably why it's said explicitly in the manual that you have to call the parent's __construct method yourself.
Generally speaking, when working with non-static methods, you'll just use $this to call methods in the same instance of either the child or the parent class ; no need to know where the method actually is.
Using parent:: works fine, even if it looks like a static call
And here's an example of code showing parent:: works fine :
class Father {
public function method() {
var_dump($this->a);
}
}
class Son extends Father {
protected $a;
public function method() {
$this->a = 10;
parent::method();
}
}
$obj = new Son();
$obj->method();
You'll get this output :
$ /usr/local/php-5.3/bin/php temp.php
int(10)
Which shows that the method in the parent class has access to $this and the properties defined in the child class.
Well, parent actually references the static parent class - there is no reason to assume there is an instantiated $parent only because there exists a $child, and even if there were, $child would not have access to $parent.
Finally, an instance where the usual class dog extends animal OOP explanations don't work! :)
Because using $parent assumes that you have actually instantiated the parent class.
If your syntax suggest worked, it would mean everytime you instantiated one object, you were instantiating 2 or more objects.
In PHP, every variable must contain a string, integer(or other numeric format), array, object, or resource. $this contains an object, and it just happens to be the object that you are currently inside.
In order to create $parent, you would have to put an object inside $parent. You parent class is technically not instantiated, so it cannot be assigned to a variable.
BTW parent::function(); has access to all of $this.
Hence, this works
class Test
{
public function test()
{
echo $this->testing_var;
}
}
class OtherTest
{
public function run()
{
$this->testing_var = "hi";
Test::test(); // echos hi
}
}
And this will error if it is used outside a class, and will tell you it should be declared static.
Test::test();