Today, We want to share with you PHP Object Oriented Programming Tutorial Example Step By Step From Scratch.In this post we will show you function overriding in php, constructor and destructor in php, hear for xname lnamePHP Classes & Objects – Introduction to OOP PHP we will give you demo and example for implement.In this post, we will learn about object-oriented php: concepts, techniques, and code pdf with an example.
PHP Object Oriented Programming Tutorial Example Step By Step From Scratch
There are the Following The simple About PHP Object Oriented Programming Tutorial Example Step By Step From Scratch Full Information With Example and source code.
As I will cover this Post with live Working example to develop Learn Object Oriented Programming (OOP) in PHP
, so the PHP Object Oriented Programming Tutorial for Beginners is used for this example is following below.
PHP Provided OOPS programming PHP Object Oriented Programming Quick Reference Guide which bundles data (properties) and related functions or methods in independent units stands for objects.
Step 1: Creating a Class
Description/examples
<?php class TamilrockersClass { // defining a behavior public $myProperty = 'a behavior value'; // defining a method public function display() { echo $this->myProperty; } } $custom_varible = new TamilrockersClass(); $custom_varible->display(); ?>
Output:
a behavior value
Step 2: Visibility (public, protected, private)
Description/examples
<?php class TamilrockersClass { public $custom_varible1 = 'public custom_varible'; protected $custom_varible2 = 'protected custom_varible'; private $custom_varible3 = 'private custom_varible'; function printHello() { echo $this->custom_varible1 . '<br>'; echo $this->custom_varible2 . '<br>'; echo $this->custom_varible3 . '<br>'; } } $obj = new TamilrockersClass(); echo $obj->custom_varible1 . '<br>'; // prints public custom_varible $obj->printHello(); // prints all // if uncommented followings will produce fatal errors /* * echo $obj->custom_varible2; // Fatal Error * echo $obj->custom_varible3; // Fatal Error */
Output:
public custom_varible public custom_varible protected custom_varible private custom_varible
Step 3: Class constants(Scope Resolution Operator (::), Using ‘self’)
Description/examples
<?php class TamilrockersClass { const PI = 3.14159; function showPI() { echo self::PI . "<br>"; } } echo TamilrockersClass::PI . "<br>"; $class = new TamilrockersClass(); $class->showPI(); echo $class::PI . "<br>"; ?>
Output:
3.14159 3.14159 3.14159
Step 4: static properties and methods(class constants, static keyword)
Description/examples
<?php class TamilrockersClass { static $custom_varible = 'a static behavior'; static function firstMthd() { return self::$custom_varible; } } echo TamilrockersClass::$custom_varible . '<br>'; echo TamilrockersClass::firstMthd(); ?>
Output:
a static behavior a static behavior
Step 5: Constructors and destructors
simple Example of the constructor and destructor in php
Description/examples
<?php class TamilrockersClass { private $prop; function __construct($custom_varible) { echo 'In constructor of ' . __CLASS__ . '<br>'; $this->prop = $custom_varible; } public function displayProp() { echo $this->prop .'<br>'; } function __destruct() { echo 'destroying ' . __CLASS__; } } $a = new TamilrockersClass('the behavior value'); $a->displayProp(); ?>
Output:
In constructor of TamilrockersClass the behavior value destroying TamilrockersClass
Step 6: Inheritance:
Description/examples
<?php class MyParentClass { protected $custom_varible = 'a super call behavior'; public function display() { echo $this->custom_varible . '<br>'; } } class MySubclass extends MyParentClass { public function getVar() { return 'returning ' . $this->custom_varible; } } $a = new MySubClass(); $a->display(); echo $a->getVar(); ?>
Output:
a super call behavior returning a super call behavior
Step 7: Inheritance and Construct/destruct
Description/examples
<?php class A { protected static $x = 'value x'; function __construct() { echo 'In constructor of ' . __CLASS__ . '<br>'; } } class B extends A { function __construct() { parent::__construct(); echo parent::$x .'<br>'; echo 'In constructor of ' . __CLASS__ . '<br>'; } } $b = new B();
Output:
In constructor of A value x In constructor of B
Step 8: Method function overriding in php
function overriding in php
Description/examples
<?php class A { function firstMthd() { return "firstMthd from A"; } } class B extends A { function firstMthd() { return "firstMthd from B, ". parent::firstMthd(); } } $a = new A; echo($a->firstMthd()); echo('<br>'); $b = new B; echo($b->firstMthd()); ?>
Output:
firstMthd from A firstMthd from B, firstMthd from A
Step 9: Abstract classes
Description/examples
<?php abstract class A{ abstract protected function firstMthd(); public function doSomething(){ $this->firstMthd(); } } class B extends A{ protected function firstMthd(){ echo 'firstMthd called'; } } $b = new B(); $b->doSomething();
Output:
firstMthd called
Step 10: Interfaces
Description/examples
<?php interface Work { public function runWork(); public function secondMthd(); } abstract class WorkImpl implements Work { public function runWork() { echo $this->secondMthd(); } } class WorkImpl2 extends WorkImpl { public function secondMthd() { return "another method running task "; } } $task = new WorkImpl2(); $task->runWork(); ?>
Output:
another method running task
Step 11: Traits
Description/examples
<?phpbehavior trait FirstTrait{ private $custom_varible; function __construct($str) { $this->custom_varible = $str; } public function aTraitFunction() { echo __METHOD__ . ", from FirstTrait: $this->custom_varible<br>"; } public function aTraitFunction2() { echo __METHOD__ . ", from FirstTrait: $this->custom_varible<br>"; } function __toString() { return __CLASS__ . ", custom_varible=$this->custom_varible <br>"; } } trait SecondTrait{ public function aTraitFunction2() { echo __METHOD__ . ", from SecondTrait: $this->custom_varible<br>"; } } class BClass { public function aTraitFunction2() { echo __METHOD__ . ", from BClass: $this->custom_varible<br>"; } } class MainClass extends BClass { use FirstTrait, SecondTrait{ SecondTrait::aTraitFunction2 insteadof FirstTrait; } public function aTraitFunction() { echo __METHOD__ . ", frome MainClass: $this->custom_varible<br>"; } } $a = new MainClass('a string'); echo $a; $a->aTraitFunction(); $a->aTraitFunction2();
Output:
MainClass, custom_varible=a string MainClass::aTraitFunction, frome MainClass: a string SecondTrait::aTraitFunction2, from SecondTrait: a string
Step 12: Final keyword
Description/examples
class A { final function display() { echo "displaying in " . __CLASS__; } } class B extends A { function display() { echo "displaying in " . __CLASS__; } } $b = new B(); $b->display();
Output:
Fatal error: Cannot override final method A::display() in D:\eclipse-workspace\php-oop\final.php on line 13
Example 2
final class A { } class B extends A { } $b = new B();
Output:
Fatal error: Class B may not inherit from final class (A) in D:\eclipse-workspace\php-oop\final-class.php on line 5
Step 13: Objects equality
Description/examples
<?php class TamilrockersClass { } function toString($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } $a = new TamilrockersClass(); $b = new TamilrockersClass(); $c = $a; echo '<br>$a==$b : ' . toString($a == $b); echo '<br>$a===$b : ' . toString($a === $b); echo '<br>$a==$c : ' . toString($a == $c); echo '<br>$a===$c : ' . toString($a === $c);
Output:
$a==$b : TRUE $a===$b : FALSE $a==$c : TRUE $a===$c : TRUE
Step 14: Object iteration
Description/examples
<?php class MovieClass { public $str = 'a str behavior'; public $int = 3; protected $str2 = "a protected str behavior"; private $str3 = "a private str behavior"; function display(){ foreach($this as $key => $value){ echo "$key => $value <br>"; } } } $a = new MovieClass(); $a->display(); echo '----<br>'; foreach($a as $key => $value){ echo "$key => $value <br>"; }
Output:
str => a str behavior int => 3 str2 => a protected str behavior str3 => a private str behavior ---- str => a str behavior int => 3
Step 15: Auto-loading Classes
Description/examples
<?php spl_autoload_register(function ($className) { echo "including $className.php <br>"; include $className . '.php'; }); $a = new A(); $b = new B();
Output:
including A.php including B.php
Step 16: String representation of objects
Description/examples
<?php class TamilrockersClass{ private $custom_varible = 'a behavior'; public function __toString(){ return "TamilrockersClass: custom_varible = '$this->custom_varible'"; } } $a = new TamilrockersClass(); echo $a;
Output:
TamilrockersClass: custom_varible = 'a behavior'
Step 17: Property Overloading
Description/examples
<?php class TamilrockersClass { private $arr; public function __set($name, $value) { $this->arr[$name] = $value; } public function __get($key) { if (array_key_exists($key, $this->arr)) { return $this->arr[$key]; } return null; } public function __isset($name) { if (isset($this->arr[$name])) { echo "Property $name is set.<br>"; } else { echo "Property $name is not set.<br>"; } } public function __unset($name) { unset($this->arr[$name]); echo "$name is unset <br>"; } } $tamilObj = new TamilrockersClass(); $tamilObj->name="joe";// will trigger __set var_dump($tamilObj); echo '<br>'; echo $tamilObj->name; //will trigger __get echo '<br>'; isset($tamilObj->name);//will trigger __isset unset($tamilObj->name);//will trigger __unset var_dump($tamilObj); ?>
Output:
object(TamilrockersClass)#1 (1) { ["arr":"TamilrockersClass":private]=> array(1) { ["name"]=> string(3) "joe" } } joe Property name is set. name is unset object(TamilrockersClass)#1 (1) { ["arr":"TamilrockersClass":private]=> array(0) { } }
Step 18: Method overloading(__call(), __callStatic)
Description/examples
<?php class TamilrockersClass { public function __call($name, $paramArr) { // do something based on method name and params return "<br>returning result of method: $name , params: " . print_r($paramArr, true) . "<br>"; } public static function __callStatic($name, $paramArr) { // do something based on method name and params return "<br>returning result of static method: $name , params: " . print_r($paramArr, true) . "<br>"; } } $a = new TamilrockersClass(); echo $a->someMethod("some arg"); echo TamilrockersClass::someStaticMethod("some arg for static method"); ?>
Output:
returning result of method: someMethod , params: Array ( [0] => some arg ) returning result of static method: someStaticMethod , params: Array ( [0] => some arg for static method )
Step 19: Calling object as a function
Description/examples
<?php class TamilrockersClass { public function __invoke() { //do something on being invoked as a function echo "invoked triggered<br>"; } } function myFunction(Callable $func) { $func(); return "returning from myFunction"; } $a = new TamilrockersClass(); //$a() this also works echo myFunction($a);
Output:
invoked triggered returning from myFunction
Step 20: Object Cloning
Description/examples
<?php class TamilrockersClass { public $custom_varible; public function __construct() { $this->custom_varible = new MyOtherClass(); } public function __clone() { $this->custom_varible = clone $this->custom_varible; } } class MyOtherClass { private $str = 'some string'; function getStr() { return $this->str; } } function toString($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } $a = new TamilrockersClass(); $b = clone $a; //using identity operator === to see two '$custom_varible' have the same references echo 'are internal objects equal: ' . toString($a->custom_varible === $b->custom_varible);
Output:
are internal objects equal: FALSE
Step 21: Serialization
Description/examples
<?php class TamilrockersClass { public $date; public $str; function __sleep() { return array('date'); } function __wakeup() { echo "wakeup triggered <br>"; } } date_default_timezone_set("America/Chicago"); $a = new TamilrockersClass(); $a->date = date("y/m/d"); $s = serialize($a); echo "serialized: <br>"; echo $s; echo "<br>"; echo "unserializing<br>"; $b = unserialize($s); echo 'unserialized object:'; echo var_dump($b); echo "<br>"; echo 'unserialized date: ' . $b->date;
Output:
serialized: O:7:"TamilrockersClass":1:{s:4:"date";s:8:"16/10/25";} unserializing wakeup triggered unserialized object:object(TamilrockersClass)#2 (2) { ["date"]=> string(8) "16/10/25" ["str"]=> NULL } unserialized date: 16/10/25
Step 22: Exporting custom_varibleiables
Description/examples
<?php class MovieClass { public $custom_varible = 4; public static function __set_state($arr) { $a = new MovieClass(); $a->custom_varible = $arr['custom_varible']; return $a; } } $a = new MovieClass(); $v = custom_varible_export($a, true); echo "exported custom_varibleiable:<br> $v <br>"; echo "<br>importing custom_varibleiable:<br>"; eval('$e=' . $v . ';'); var_dump($e);
Output:
exported custom_varibleiable: MovieClass::__set_state(array( 'custom_varible' => 4, )) importing custom_varibleiable: object(MovieClass)#2 (1) { ["custom_varible"]=> int(4) }
Step 23: Controlling debug info
Description/examples
class MovieClass { private $custom_varible = 10; private $str = 'a string'; public function __debugInfo() { return ['string' => $this->str]; } } $a = new MovieClass(); var_dump($a);
Output:
object(MovieClass)#1 (1) { ["string"]=> string(8) "a string" }
Web Programming Tutorials Example with Demo
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Object Oriented Programming Tutorial Example Step By Step From Scratch.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donβt forget to share.