Quickupdate

  • Home
  • Top Trending
    • Top Android Apps
    • Top Ios Apps
  • Featured
  • About Us
  • Contact us
  • Privacy Policy and Disclaimer

Friday, 10 January 2020

PHP Oops - Static Properties / Keword

 Developers     January 10, 2020     No comments   

1) there are many variations of static keyword
1) static method
2) static property
3) static closure (A CLOSURE is an anonymous function that can access variables imported from the outside scope without using any global variables.)
4) static variable
5) static as a class name

2) $this isn't accessible in these methods

3) IF you create a static property/method THen you don't have to create/instantiate a class. you can access without creating the object of a class.

4)if you declared a static property and you created obj of class, then you cannot access that property. (but you can access, Methods)
E.g:

<?php

  class StaticClassExample {
  public static function test() {
  echo "this is the static function";
  }
}

new StaticClassExample();
StaticClassExample::test();
?>

5) To access a static property use the class name, a double colon (::), and the property name
ClassName::staticPropName();

6) A class can have both static and non-static Properties. A static Property can be accessed from a METHOD in the SAME class using
the "self" keyword and double colon "(::)".

7) You can create 2 classes in One File

<?php
class StaticClassExample
{

    public static function test()
    {
        echo "this is the Static function.<br>";
    }

    public function testOne()
    {
        echo "This is the Normal function.<br>";
    }
}

StaticClassExample::test();
// $a = new StaticClassExample();
// $a->testOne();


class NewClass extends StaticClassExample
{

    public function get_value_from_static()
    {
        echo parent::testOne();
    }
}

$b = new NewClass();
$b->get_value_from_static();
?>

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Popular Posts

  • How to upload the existing folder on GitHub Or GitLab?
    These are the steps to upload the existing folder on GitHub Or GitLab. Whenever you want to push your existing folder to git or GitHub you m...

Categories

  • FAANG (2)
  • Javascript (6)
  • Node (1)
  • Project Management (1)
  • React (9)
  • SQL (1)
  • Testing (1)

Blog Archive

  • January 2023 (1)
  • January 2022 (1)
  • November 2021 (3)
  • October 2021 (3)
  • August 2021 (1)
  • July 2021 (7)
  • June 2021 (12)
  • February 2021 (1)
  • January 2021 (1)
  • January 2020 (3)
  • August 2019 (3)