3. Access modifier/ Visibility

Access modifier/ Visibility:

By default, all class members are public. This means if you do not declare a property or method within the class,it will be public. It is good practice to declare the visibility of class members for the sake of readability for yourself and others. It is much easier for another programmer to see your intentions.public class members (properties and methods) are available through-out the script and may be accessed from outside the class as we demonstrated in our car class. Lets create a simple new class to demonstrate.





2. OOP- Constructors & Destructor

If you create a __construct() function (it is your choice,) PHP will automatically
call the __construct() method/function when you create an object from your class.

The ‘construct’ method starts with two underscores (__) and the word ‘construct’. You ‘feed’ the constructor method by providing a list of arguments (like a function) after the class name.




1. Object Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a type of programming added to php5,With the release of php5, php programmers finally had the power to code with the ‘big boys’. Like Java and C#, php finally has a complete OOP infrastructure.

Before we move onto writing object-oriented code in PHP, it’s important to understand 4 fundamental OOP concepts: classes, objects, properties, and methods.

A class is a blueprint for objects. When you build your object-oriented application, you typically create one or more classes representing various types of entities in your app.For example, if you’re writing a forum application, you might create classes called Forum, Topic, Post, and Member.


You define your own class by starting with the keyword ‘class’ followed by the name
you want to give your new class.

<?php class person { } ?>

This class doesn’t do much until you’ve added properties and methods to the class.
Nevertheless, the above code creates a valid, usable class in PHP.

[ It’s good programming practice to put each class’s code in its own file, with the same name as the class.For example, you might put the above Member class code in a file called Member.php, and store the file in a folder called classes. ]

An object is a special type of variable that is created from a class.It contains actual data, and you can call the object’s functions to do stuff to that data.You can create as many objects as you like from a single class.Each object functions independently of the others, even if they all come from the same class.

To use a real-world analogy:
. A class is like a blueprint for a car. It defines how cars created from the blueprint will look and behave, but it’s still an abstract concept.
. An object is like a real car created from the blueprint. It has real properties (such as how fast it’s going), and real behaviors (like “accelerate” and “brake”).


How to create objects in PHP:
You create an object from a class by using the new keyword, as follows:

<?php $stefan = new person(); ?>

Class member variables are called “properties”.They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.This declaration may include an initialization,but this initialization must be a constant value–that is,it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property).
Static properties are accessed by using the :: (Double Colon): self::$property.
Scope Resolution Operator (::) (also called Paamayim Nekudotayim)

The ‘$this’ variable:
You probably noticed this line of code: $this->name = $new_name The $this is a built-in pseudo variable (built into all objects) which points to the current object. Or in other words, $this is a special self-referencing variable. You use $this to access properties and to call other methods of the current class.

The functions that are defined within a class — and used in an object — are known as methods.In many ways, they’re just like regular functions — you can pass values to them,they can contain local variables, and they can return values.