How to invoke a method statically?

<?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 $this keyword 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();

    链接地址: http://www.djcxy.com/p/58040.html

    上一篇: self ::引用父类的静态方法中的派生类

    下一篇: 如何静态调用方法?