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();
?>
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();
?>
0 comments:
Post a Comment