Facebooktwittergoogle_plusredditpinterestlinkedinmail

$this vs self in php:

$this refers to the current object of the class and self refers to the current class itself.
$this can not be used inside static function whereas self can be used inside static function.

When we need to access static function and reference static member variable then we can’t access through $this because a property declared as static cannot be accessed with an instantiated class object.

In below example , I explain how we can access a variable using regularMethod and staticMethod. To access static properties or method we don’t need to create object of the class. We can access it using class name scope resolution operator (::) and property or method name ( e.g fooBar::$staticVar, fooBar::staticMethod() )

<?php
class fooBar
{
   public $var;
   public static $staticVar;

   public function regularMethod() {
      echo $this->var;
   }
   /**
   * Demonstration: static method and usages of static variable.
   */
   public static function staticMethod() {
      echo self::$staticVar;
     // echo $this->staticVar;
     //  If you are try to use $this here you will get
     //  Fatal error: Using $this when not in object context....
   }
}

/**
* Declaring class properties or methods as static makes them accessible
* without needing an instantiation of the class.
* AS we set $staticVar variable value without creating object of fooBar class
*/
fooBar::$staticVar = "Static Variable Value , Called by staticMethod";
$obj = new fooBar();
$obj->var = "Regular Variable, called by regularMethod";
// static methods are callable without an instance of the object created
echo fooBar::staticMethod(); // Output : Static Variable Value , Called by staticMethod
echo $obj->regularMethod(); // Output //
?>
$this vs self in php Roopendra PHP,
$this vs self in php: $this refers to the current object of the class and self refers to the current class itself. $this can not be used inside static function whereas self can be used inside static function.When we need to access static function and reference static member variable then we...
<h2>$this vs self in php:</h2> <strong>$this</strong> refers to the current object of the class and <strong>self</strong> refers to the current class itself. <strong>$this</strong> can not be used inside static function whereas <strong>self</strong> can be used inside static function.When we need to access static function and reference static member variable then we can't access through <strong>$this</strong> because a property declared as static cannot be accessed with an instantiated class object.In below example , I explain how we can access a variable using <strong>regularMethod</strong> and <strong>staticMethod</strong>. To access static properties or method we don't need to create object of the class. We can access it using class name scope resolution operator <strong>(::)</strong> and property or method name ( e.g <em>fooBar::$staticVar, fooBar::staticMethod()</em> ) <pre><code><?php class fooBar { public $var; public static $staticVar;public function regularMethod() { echo $this->var; } /** * Demonstration: static method and usages of static variable. */ public static function staticMethod() { echo self::$staticVar; // echo $this->staticVar; //  If you are try to use $this here you will get //  Fatal error: Using $this when not in object context.... } }/** * Declaring class properties or methods as static makes them accessible * without needing an instantiation of the class. * AS we set $staticVar variable value without creating object of fooBar class */ fooBar::$staticVar = "Static Variable Value , Called by staticMethod"; $obj = new fooBar(); $obj->var = "Regular Variable, called by regularMethod"; // static methods are callable without an instance of the object created echo fooBar::staticMethod(); // Output : Static Variable Value , Called by staticMethod echo $obj->regularMethod(); // Output // ?></code></pre>
(Visited 249 times, 4 visits today)
Facebooktwittergoogle_pluslinkedinrss