OOPs Tutorials
- What is OOPs
- Access Modifier
- Constructor
- Destructor
- Polymorphism
- Inheritence
- Method Overriding
- Abstract class
- Interface
- Traits
- This keyword
- static keyword
- Final keyword
- Class Constant
- Exception Handling
- Binding in php
- tostring in php
- Object Iteration
- Reflection
- Magic methods
- Namespaces
- CRUD Function
- OOPs CRUD
- CRUD Example
Important Links
What is static keyword?
Creating static method
When you create static methods, you can do that call the method having to first create an object of that class. Static methods are class methods, they are meant to called on the class, not on an object.
<?php
Class Math
{
public static function say( )
{
echo “ I am mahtab . <br>”;
}
}
?>
Now you can access the say( ) in the Math class without needing to first create an object of that class.
<?php
Class Math
{
public static function say( )
{
echo “ I am mahtab . <br>”;
}
}
echo “Using the math class… <br>”;
Math::say( );
?>
Note :- the syntax :
Here Math::say( ). You can’t use the -> operator here.
When working with an object ,because no object is involved. Instead, you use the double colon operator (::) also called the scope resolution operator.
Passing data to a static method :
Say that you wanted to write the square method and add it to the Math class. You pass that method the number you want to square.
Here how to call and pass data to the squarer method in program.
class Math
{
static $data;
public static function say()
{
echo "hi mahtab";
}
static function squrare($op)
{
echo $op, "<sup>2</sup> = ", $op * $op, "<br>";
}
}
echo "using the Math class" ."<br>";
Math::say();
Math::squrare(2);
?>
Output :
using the Math classhi mahtab22 = 4
Static function use only static variable.
class Math
{
static $data;
public static function say()
{
echo "hi mahtab";
}
function squrare($op)
{
echo $op, "<sup>2</sup> = ", $op * $op, "<br>";
}
}
echo "using the Math class" ."<br>";
Math::say();
Math::squrare(2);
?>
Using properties in static method:
Some time you want to use porperties when you do your work, and that’s no problem. When you’re using objects. But can you use properties when you’re just working with the class in code, not object. As long as they’re static properties.
For example
You had a static method named set_data( ) that’s passed an argument.
Now say that you want to save the data passed to this static method in a class property.
<?php
class Math
{
static $data;
public static function set_data($op)
{
}
}
?>
Now you are free to store data in that property. you can’t use the $this->data syntax, because $this refer to the current object. And there is no object here. Instead, you have to use the keyword self when working with static properties in static methods. So here’s how you can store the data passed to set_data in the $data property.
class Math
{
static $data;
public static function set_data($op)
{
Self::$data = $op;
echo “self::\$data = “, self::$data, “<br>”;
echo “adding one to self::\$data <br>”;
Self::$data++;
echo “Now self::\$data = “, self::$data,”<br>”;
}
}
echo "using the Math class" ."<br>";
Math::set_data(5);
?>
File name : index.php
File name : index.php