In this example I get the fatal error "Fatal error: Using $this when not in object context" as expected
class ctis_parent{
public function objFunc(){
var_dump('Called succes');
}
public static function call(){
$this->objFunc();
}
public function __construct(){
self::call();
}
}
new ctis_parent();
But if remove static keyword from definition of call() method all work fine, why?
class ctis_parent{
public function objFunc(){
var_dump('Called succes');
}
public function call(){
$this->objFunc();
}
public function __construct(){
self::call();
}
}
new ctis_parent();
//string 'Called succes' (length=13)
A static function, by definition doesn't need the class being instantiated, so it doesn't have access to the $this-> reference, which points to the current instance. If an instance doesn't exist, it can't be pointed to. Makes sense.
Because you're using $this when you're not in an object. Well, you're not REALLY, but with the static declaration, people could do:
ctis_parent::call();
Where, $this would be illegal.
See the docs for static.
Related
I'm learning Laravel and the way I learn a new framework is going deep and found how/why magic happened in that.
So I learn facades and love them and wanna found how Laravel do the magic and found a way to have similar feature is __callStatic magic method. and here is my code :
class Facade {
public static function __callStatic($method,$args){
$instance = static::getFacade();
return call_user_func_array([$instance, $method], $args);
}
}
class DB extends Facade {
public static function getFacade(){
return new self();
}
public function init(){
echo 'INIT DB';
}
}
DB::init();
the code has a right output but I get this error:
Non-static method DB::init() should not be called statically :21
I don't understand why I got this and also Why I don't get this error in Laravel application that does this.
As init() is your public method and not static, you can call it via its object.
Try to call it by creating object of class DB.
$obj = new DB();
$obj->init();
Hope it will help you.
This method doen't have static keyword so you've to call it using the instance of that class and have to use this -> operator instead of ::
//this is a static method with static keyword preceding function
public static function getFacade(){
^^^^^^
return new self();
}
// this is non-static method with no static keyword
public function init(){
echo 'INIT DB';
}
Non-Static: create an instance and then call it using ->
$instance = new DB();
$instance->init();
Static: No need to create instance just call it using ::
DB::getFacade()
Because __callStatic method only catches inaccessible or non-exist methods so if you change
public function init(){
echo 'INIT DB';
}
To protected it will work
protected function init(){
echo 'INIT DB';
}
Also I asked a question before about this and I think it might be helpful for you
Here is my question
<?php
class baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
public function test()
{
static::show();
}
}
class AModel extends baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
}
class Controller
{
public function actionTest()
{
baseModel::test();
AModel::test();
}
}
$c = new Controller;
$c->actionTest();
?>
Expected output:
baseModelAModel
Actual output:
Fatal error: Call to undefined method Controller::show() in /var/www/dashboard/test.php on line 12
Why did PHP try to find Controller::show() rather than AModel::show?
static keyword refers to context, not the class the method was defined in. So, when you call A::test from context of C, static refers to C.
As it writes in php manual
In non-static contexts, the called class will be the class of the object instance. Since $this-> will try to call private methods from the same scope, using static:: may give different results. Another difference is that static:: can only refer to static properties.
This means that in context of object $c (class Controller) making a non-static call ($c->t()) late binding static:: keyword in method baseModel::test() references class of object reference $this which is Controller and not called class, while if calling static:
Controller::test();
Context of static:: is called class.
I would however advise you not to use static calls unless methods are explicitly defined as static:
class baseModel
{
public static function show($className=__CLASS__)
{
print_r($className);
}
public static function test()
// must be defined as static or context of static:: may change to Controller instead of actual class being called!
{
static::show();
}
}
This code works because if defining explicitly baseModel::test() as being static context of static:: will always be static.
<?php
class Popular
{
public static function getVideo()
{
return $this->parsing();
}
}
class Video
extends Popular
{
public static function parsing()
{
return 'trololo';
}
public static function block()
{
return parent::getVideo();
}
}
echo Video::block();
I should definitely call the class this way:
Video::block();
and not initialize it
$video = new Video();
echo $video->block()
Not this!
Video::block(); // Only this way <<
But:
Fatal error: Using $this when not in object context in myFile.php on line 6
How to call function "parsing" from the "Popular" Class?
Soooooooory for bad english
As your using a static method, you cant use $thiskeyword as that can only be used within objects, not classes.
When you use the new keyword, your creating and object from a class, if you have not used the new Keyword then $this would not be available as its not an Object
For your code to work, being static you would have to use the static keyowrd along with Scope Resolution Operator (::) as your method is within a parent class and its its not bounded, Use the static keyword to call the parent static method.
Example:
class Popular
{
public static function getVideo()
{
return static::parsing(); //Here
}
}
What does $this mean in PHP?
paamayim-nekudotayim - Scope Resolution
http://php.net/manual/en/language.oop5.static.php
change return $this->parsing(); to return self::parsing();
I got 2x public functions in a class that must call 1 private function with different parameters also in the same class... for some reason it tell me that it can't find the function...
example:
class Foo {
private function Do(...)
{
....
return $whatever;
}
public function One(...)
{
return Do(...);
}
public function Two(...)
{
return Do(...);
}
}
am getting error:
Fatal error: Call to undefined function do() in ...
You have to use $this to refer to the instance and the T_OBJECT_OPERATOR to access/mutate/call members/methods of an instance, e.g.
$this->do();
Please go through the
Chapter on Classes and Objects in the PHP Manual and
What is the point of having $this and self:: in PHP?
I have a question regarding static function in php.
let's assume that I have a class
class test {
public function sayHi() {
echo 'hi';
}
}
if I do test::sayHi(); it works without a problem.
class test {
public static function sayHi() {
echo 'hi';
}
}
test::sayHi(); works as well.
What are the differences between first class and second class?
What is special about a static function?
In the first class, sayHi() is actually an instance method which you are calling as a static method and you get away with it because sayHi() never refers to $this.
Static functions are associated with the class, not an instance of the class. As such, $this is not available from a static context ($this isn't pointing to any object).
Simply, static functions function independently of the class where they belong.
$this means, this is an object of this class. It does not apply to static functions.
class test {
public function sayHi($hi = "Hi") {
$this->hi = $hi;
return $this->hi;
}
}
class test1 {
public static function sayHi($hi) {
$hi = "Hi";
return $hi;
}
}
// Test
$mytest = new test();
print $mytest->sayHi('hello'); // returns 'hello'
print test1::sayHi('hello'); // returns 'Hi'
Entire difference is, you don't get $this supplied inside the static function. If you try to use $this, you'll get a Fatal error: Using $this when not in object context.
Well, okay, one other difference: an E_STRICT warning is generated by your first example.
Calling non-static methods statically generates an E_STRICT level warning.
In a nutshell, you don't have the object as $this in the second case, as
the static method is a function/method of the class not the object instance.
After trying examples (PHP 5.3.5), I found that in both cases of defining functions you can't use $this operator to work on class functions. So I couldn't find a difference in them yet. :(