0
<?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

Gordon
  • 312,688
  • 75
  • 539
  • 559
Isis
  • 4,608
  • 12
  • 39
  • 61
  • on a related note, read about late static bindings. http://php.net/manual/en/language.oop5.late-static-bindings.php – zzzzBov Nov 09 '10 at 16:27
  • You should avoid static methods if possible. [They are death to testability](http://misko.hevery.com/2008/12/15/static-methods-are-death-to-testability). – Gordon Nov 09 '10 at 16:28
  • You can not use $this in static context. Use self as suggested below. – Vikash Nov 09 '10 at 16:30
  • *(related)* [In PHP, whats the difference between :: and -> ?](http://stackoverflow.com/questions/3173501/in-php-whats-the-difference-between-and) and [What exactly is late-static binding in PHP?](http://stackoverflow.com/questions/1912902/what-exactly-is-late-static-binding-in-php) – Gordon Nov 09 '10 at 16:33
  • possible duplicate of [PHP Fatal error: Using $this when not in object context](http://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context) – Gordon Nov 09 '10 at 16:51

2 Answers2

2

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
    }
}
Community
  • 1
  • 1
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
1

change return $this->parsing(); to return self::parsing();

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • `self::parsing()` will try to call `Popular::parsing()`, which doesn't exist. If using PHP 5.3.0 (or later), you should use late static bindings, i.e. `static::parsing()`. – Aether Nov 09 '10 at 16:29