Classes and Objects

Introduction

Class is as like a blueprint for a house . and Objects is actual house following by buleprint .

Class is declared by class key and pair of {} curly braces

Basics

Propertis

Class member variables are property . Property type public, protected, and private .

Accessing property inside method

Class constants

Constant variable means variable value is always unchangable . Constant variable is defined by const without $ sign . Constant visibility is always public .

<?php
class User
{
const DefaultUsername = "John Doe";
const MinimumPasswordLength = 6;
}
echo "The default username is " . User::DefaultUsername;
echo "The minimum password length is " . User::MinimumPasswordLength;
?>

Autoloading Classes

Constructor And Destructor

A constructor and a destructor are special functions which are automatically called when an object is created and destroyed. The constructor is the most useful of the two, especially because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object. Here’s an example of a class with a simple constructor:

<?php
class Animal
{
public $name = "No-name animal";
public function __construct($name)
{
$this->name = $name;
}
}
$animal = new Animal("Bob the Dog");
echo $animal->name;
?>

Destructors
A destructor is called when the object is destroyed. In some programming languages, you have to manually dispose of objects you created, but in PHP, it’s handled by the Garbage Collector, which keeps an eye on your objects and automatically destroys them when they are no longer needed. Have a look at the following example, which is an extended version of our previous example:

<?php
class Animal
{
public $name = "No-name animal";
public function __construct($name)
{
echo "I'm alive!";
$this->name = $name;
}
public function __destruct()
{
echo "I'm dead now :(";
}
}
$animal = new Animal("Bob");
echo "Name of the animal: " . $animal->name;
?>

Visibilty

The visibility of a property, a method or (as of PHP 7.1.0) a constant can be defined by prefixing the declaration with the keywords public, private or protected.
Class

Object Inheritance

One of the main advantages of object-oriented programming is the ability to reduce code duplication with inheritance. Code duplication occurs when a programmer writes the same code more than once, a problem that inheritance strives to solve. In inheritance, we have a parent class with its own methods and properties, and a child class (or classes) that can use the code from the parent. By using inheritance, we can create a reusable piece of code that we write only once in the parent class, and use again as much as we need in the child classes.

In order to declare that one class inherits the code from another class, we use the extends keyword. Let’s see the general case:

class Parent {
// The parent’s class code
}
class Child extends Parent {
// The child can use the parent's class code
}

Scope resolution operator (::)

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

Three special keywords self, parent and static are used to access properties or methods from inside the class definition.

<?php
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
}
$classname = 'OtherClass';
$classname::doubleColon(); // As of PHP 5.3.0
OtherClass::doubleColon();
?>

Static Keyword

Static methods and properties in php is very useful feature. Static methods and properties in php can directly accessible without creating object of class. Your php class will be static class if your all methods and properties of the class is static. Static Methods and Properties in PHP will be treated as public if no visibility is defined.

class test
{
public static $a;//Static variable
}
test::$a = 5;
echo test::$a;

Class Abstraction

An abstract class is a class that has at least one abstract method. Abstract methods can only have names and arguments, and no other code. Thus, we cannot create objects out of abstract classes. Instead, we need to create child classes that add the code into the bodies of the methods, and use these child classes to create
objects.

// Abstract classes are declared with the abstract keyword, and contain abstract methods.
abstract class Car {
abstract public function calcNumMilesOnFullTank();
}

Object Interfaces

An interface allows you to specify a list of methods that a class must implement. To define an interface, you use the interface keyword as follows:

<?php
interface IMyInterface{
public function method_1();
public function method_2();
}
class MyClass implements IMyInterface{
public function method_1(){
// method 1 implementation
}
public function method_2(){
// method 2 implementation
}
}

<?php
interface IReadable{
public function read();
}
interface IWritable extends IReadable{
public function write();
}
class MyClass implements IWritable(){
public function read(){
// read implementation
}
pubic function write(){
// write implemention
}
}

Trait

Anonymous classes

Overloading

PHP’s interpretation of “overloading” is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.

magic method is use for overloading in php.

Object Iteration

Magic Methods

The magic methods are ones with special names, starting with two underscores, which denote methods which will be triggered in response to particular PHP events

public function __construct()
public function __destruct()
public function __get()
public function __set()
public function __call()
public function __sleep()
public function __wakeup()
public function __clone()
public function __toString()
public function __invoke()
public function __set_state()
public function __call()
public function __callStatic()
public function __isset()
public function __unset()
public function __debugInfo()

Final keywords

In PHP, Final Keyword is applicable to only class and class methods. We can not declare variables as Final in PHP.So if you declare class method as a Final then that method can not be override by the child class. Same as method if we declare class as a Final then that class can not be extended any more.

Below code will end up with this error: Cannot override final method parent_class::class_method()

class parent_class
{
// We Can not override this method
// As it declared as final
final public function class_method()
{
/*
Code Here
*/
}
}
class child_class extends parent_class
{
public function class_method()
{
/*
Code Here
*/
}
}

Object Cloning

reating a copy of an object with fully replicated properties is not always the wanted behavior. A good example of the need for copy constructors, is if you have an object which represents a GTK window and the object holds the resource of this GTK window, when you create a duplicate you might want to create a new window with the same properties and have the new object hold the resource of the new window. Another example is if your object holds a reference to another object which it uses and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.

An object copy is created by using the clone keyword (which calls the object’s __clone() method if possible). An object’s __clone() method cannot be called directly.

$copy_of_object = clone $object;

class test
{
public $a;
private $b;
function __construct($a, $b)
{
$this->a = $a;
$this->b = $b;
}
}
$a = new test("ankur" , "techflirt");
$b = $a; //Copy of the object
$c = clone $a; //clone of the object
$a->a = "no Ankur";
print_r($a);
print_r($b);
print_r($c);

Comparing Object

When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.

When using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.

Type Hinting

Late Static Bindings

Objects and references

Object Serialization

OOP Changelog