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 OOPs?
OOPs is a concept or mechanism. which is used for developing the application software.
these concept are :-
Class :-
File name : index.php
Class Person
{
var $name;
function set_name($data)
{
// statements....
}
}
Now you have got to store the name passed to the set_name function in the $name variable. You could do that, by accessing $name as a global variable. Then you can assign the agrument passed to set_name , $data, to the internally stored name, $name, like that
File name : index.php
class Person
{
var $name;
function set_name($data)
{
global $name;
$name = $data;
}
}
That store the person’s name. you will also need some way to read the person’s name, so you might add another method called get_name.
File name : index.php
class Person
{
var $name;
function set_name($data)
{
global $name;
$name = $data;
}
function get_name()
{
global $name;
return $name;
}
}
You won’t normally see OOP done using the globle keyword. Instead, you usually refer to the properties to the class using the $this keyword. The $this keyword points to the current object.
in php terms, this :
Global $name;
$name = $data;
Can be replaced by this way :
$this->name = $data;
NOTE :- $this is followed by the -> operator.
Object
When you use the class to build an object this process is known as instantiation. Instantiating an object requires two things:
File name : index.php
<?php
class Demo
{
// data member
// member function
}
?>
You can instantiate an object of type Demo class.
<?php
require_once(‘class.Demo.php’);
$obj = new Demo();
?>
Here $obj is the object of the Demo class.
Adding a method in class :-
File name : index.php
<?php
class Demo
{
function sayhello(
{
echo "hi mahtab";
}
}
$obj = new Demo();
$obj->sayhello();
?>
File name : index.php
<?php
class Demo
{
function sayhello()
{
echo "hi mahtab";
}
}
?>
<?php
$obj = new Demo();
$obj->sayhello();
?>
Output :-
Hi mahtab
File name : index.php
<?php
class Demo
{
function sayhello()
{
echo "hi mahtab";
}
$obj = new Demo();// here error occurs, we can't create the object under the body of class.
$obj->sayhello(); // error
}
?>
Note :-
Echo and print funtion use only within function and outside the class.
Parametrized function
File name : index.php
<?php
class Demo
{
function sayhello($name)
{
echo "hi $name";
}
}
$obj = new Demo();
$obj->sayhello('mahtab_habib');
?>
Output :-
Hi mahtab_habib
File name : index.php
NOTE :-
The -> operator is used to access all methods and properties of an objects.
Php does not use the dot (.) operator in its oop syntax at all.
You simply declare a variable inside the class to hold the value of the property. This variable is declared at the top of the class declaration, inside the braces that bracket the class's code. The name of the variable is the name of the property. If the variable is called $color, you will have a property called color.
File name : index.php
<?php
class Demo
{
public $name;
function sayhello()
{
print "Hello $this->name"; // it is declared by $name but access by only name.
}
}
$obj = new Demo();
$obj->name = 'mahtab';
$obj1 = new Demo();
$obj1->name='danish';
$obj->sayhello();
$obj1->sayhello();
?>
Output : Hello mahtab
Hello Danish
File name : demo.php
<?php
class Demo
{
public $name;
function sayhello()
{
print "Hello $this->name";
}
}
?>
File name : test.php
<?php
Require_once('class.demo.php');
$obj = new Demo();
$obj->name = 'mahtab';
$obj1 = new Demo();
$obj1->name='danish';
$obj->sayhello();
$obj1->sayhello();
?>
NOTE :-