php oop concepts code example

Example 1: object oriented programming php

<?php
class Parent {
	public function __construct() {
    	echo "Parent Created\n";
    }
  	public function sayHello() {
    	echo "Hello, from Parent\n";
    }
  	public function eitherHello() {
    	echo "Hello, from Parent and child if desired\n";
    }
}
class Child extends Parent {
	public function __construct() {
    	echo "Child Created\n";
    }
  	public function sayHello() {
    	echo "Hello, from Child\n";
    }
}
$p = new Parent();	// Parent Created
$c = new Child();	// Child Created
$p->sayHello(); 	// Hello, from Parent
$c->sayHello();		// Hello, from Child
$p->eitherHello();	// Hello, from Parent and child if desired
$c->eitherHello();	// Hello, from Parent and child if desired
?>

Example 2: php object

$o= new \stdClass();
$o->a = 'new object';

OR

$o = (object) ['a' => 'new object'];

Example 3: oops concepts in php

The  PHP Object-Oriented Programming concepts are:
Class 
Objects
Inheritance
Interface
Abstraction
Magic Methods

Tags:

Php Example