There is a class with such methods.

private function GetBannerInf() { return 123; } static function GetPathBanner() { return $this->GetBannerInf(); } 

Does not work. gives the error Fatal error: Using $ this when not in object context ...

But if you write like this: return self :: GetBannerInf ();

Question 1, why self :: GetBannerInf (); if the GetBannerInf () method is not static. Question 2, why the first option does not work through this?

  • one
    I certainly not pkhshshnik, but from where at static function this ? That's why it is static, which can be called even by the name of a class, without an object. - pavel
  • thanks bros, handshake. And how, in my case, would it be more correct to hide GetBannerInf and call it via static GetPathBanner? - aat
  • as an option, too, make static. - pavel
  • Static and private? What is it like? - aat
  • The scope is determined, not to hide the method, but to delineate the rights (access) to a particular method. What is the basis of the principle of Абстракции . Your question goes against the principles of oop. In this particular example or case, you cannot call this method via $this or new self , etc. Since it violates the principles of this oop. At a minimum, you must change the scope of the method and call as 'new static' or 'new self' and later call from the object, or call the method as is. - Naumov

1 answer 1

  1. The self call goes like a class, not a specific object of this class. And the class has this method. Although it throws an error about calling a non-static method as static.
  2. $this is a pointer to a specific class object. self is a pointer to a class in general. In static methods, for obvious reasons, there can be no pointer to a specific object, and that’s an error.