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
myProperty; } } $custom_varible = new TamilrockersClass(); $custom_varible->display(); ?>
Output:
a behavior value
Step 2: Visibility (public, protected, private)
Description/examples
custom_varible1 . '
'; echo $this->custom_varible2 . '
'; echo $this->custom_varible3 . '
'; } } $obj = new TamilrockersClass(); echo $obj->custom_varible1 . '
'; // 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
"; } } echo TamilrockersClass::PI . "
"; $class = new TamilrockersClass(); $class->showPI(); echo $class::PI . "
"; ?>
Output:
3.14159 3.14159 3.14159
Step 4: static properties and methods(class constants, static keyword)
Description/examples
'; 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
'; $this->prop = $custom_varible; } public function displayProp() { echo $this->prop .'
'; } 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
custom_varible . '
'; } } 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
'; } } class B extends A { function __construct() { parent::__construct(); echo parent::$x .'
'; echo 'In constructor of ' . __CLASS__ . '
'; } } $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
firstMthd()); echo('
'); $b = new B; echo($b->firstMthd()); ?>
Output:
firstMthd from A firstMthd from B, firstMthd from A
Step 9: Abstract classes
Description/examples
firstMthd(); } } class B extends A{ protected function firstMthd(){ echo 'firstMthd called'; } } $b = new B(); $b->doSomething();
Output:
firstMthd called
Step 10: Interfaces
Description/examples
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
custom_varible = $str; } public function aTraitFunction() { echo __METHOD__ . ", from FirstTrait: $this->custom_varible
"; } public function aTraitFunction2() { echo __METHOD__ . ", from FirstTrait: $this->custom_varible
"; } function __toString() { return __CLASS__ . ", custom_varible=$this->custom_varible
"; } } trait SecondTrait{ public function aTraitFunction2() { echo __METHOD__ . ", from SecondTrait: $this->custom_varible
"; } } class BClass { public function aTraitFunction2() { echo __METHOD__ . ", from BClass: $this->custom_varible
"; } } class MainClass extends BClass { use FirstTrait, SecondTrait{ SecondTrait::aTraitFunction2 insteadof FirstTrait; } public function aTraitFunction() { echo __METHOD__ . ", frome MainClass: $this->custom_varible
"; } } $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
$a==$b : ' . toString($a == $b); echo '
$a===$b : ' . toString($a === $b); echo '
$a==$c : ' . toString($a == $c); echo '
$a===$c : ' . toString($a === $c);
Output:
$a==$b : TRUE $a===$b : FALSE $a==$c : TRUE $a===$c : TRUE
Step 14: Object iteration
Description/examples
$value){ echo "$key => $value
"; } } } $a = new MovieClass(); $a->display(); echo '----
'; foreach($a as $key => $value){ echo "$key => $value
"; }
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
"; include $className . '.php'; }); $a = new A(); $b = new B();
Output:
including A.php including B.php
Step 16: String representation of objects
Description/examples
custom_varible'"; } } $a = new TamilrockersClass(); echo $a;
Output:
TamilrockersClass: custom_varible = 'a behavior'
Step 17: Property Overloading
Description/examples
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.
"; } else { echo "Property $name is not set.
"; } } public function __unset($name) { unset($this->arr[$name]); echo "$name is unset
"; } } $tamilObj = new TamilrockersClass(); $tamilObj->name="joe";// will trigger __set var_dump($tamilObj); echo '
'; echo $tamilObj->name; //will trigger __get echo '
'; 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
returning result of method: $name , params: " . print_r($paramArr, true) . "
"; } public static function __callStatic($name, $paramArr) { // do something based on method name and params return "
returning result of static method: $name , params: " . print_r($paramArr, true) . "
"; } } $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
"; } } 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
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
"; } } date_default_timezone_set("America/Chicago"); $a = new TamilrockersClass(); $a->date = date("y/m/d"); $s = serialize($a); echo "serialized:
"; echo $s; echo "
"; echo "unserializing
"; $b = unserialize($s); echo 'unserialized object:'; echo var_dump($b); echo "
"; 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
custom_varible = $arr['custom_varible']; return $a; } } $a = new MovieClass(); $v = custom_varible_export($a, true); echo "exported custom_varibleiable:
$v
"; echo "
importing custom_varibleiable:
"; 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.