0% found this document useful (0 votes)
15 views

Some Basic OO Terms

Uploaded by

Yatin Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Some Basic OO Terms

Uploaded by

Yatin Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Some Basic OO Terms

Some of the basic object-oriented terms are as follows:


Class: A class is a template for an object. A class contains the code which defines how an object
will behave and interact either with each other, or with it. Every time you create an object in
PHP, you are actually developing the class. So sometimes in this book we will name an object as
class, as they are both synonymous.

Property: A property is a container inside the class which can retain some information. Unlike
other languages, PHP doesn't check the type of property variable. A property could be accessible
only in class itself, by its subclass, or by everyone. In essence, a property is a variable which is
declared inside the class itself, but not inside any function in that class.
Method: Methods are functions inside a class. Like properties, methods can also be accessible by
those three types of users.
Encapsulation: Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse. The wrapping up of
data and methods into a single unit (called class) is known as encapsulation. The benefit of
encapsulating is that it performs the task inside without making you worry.

Polymorphism: Objects could be of any type. A discrete object can have discrete properties and
methods which work separately to other objects. However a set of objects could be derived from
a parent object and retain some properties of the parent class. This process is called
polymorphism. An object could be morphed into several other objects retaining some of its
behaviour.
Inheritance: The key process of deriving a new object by extending another object is called
inheritance. When you inherit an object from another object, the subclass (which inherits) derives
all the properties and methods of the superclass (which is inherited). A subclass can then process
each method of superclass anyway (which is called overriding).
Coupling: Coupling is the behaviour of how classes are dependent on each other. Loosely
coupled architecture is much more reusable than tightly coupled objects. In the next chapter we
will learn details about coupling. Coupling is a very important concern for designing better
objects.
Design Patterns: First invented by the "Gang of Four", design patterns are just tricks in object
oriented programming to solve similar sets of problems with a smarter approach. Using design
patterns (DP) can increase the performance of your whole application with minimal code written
by developers. Sometimes it is not possible to design optimized solutions without using DP. But
unnecessary and unplanned use of DP can also degrade the performance of your application. We
have a chapter devoted for design patterns in this book.
Subclass: A very common term in OOP, and we use this term throughout this book. When an
object is derived from another object, the derived one is called the subclass of which it is derived
from.
Superclass: A class is superclass to an object if that object is derived from it. To keep it simple,
when you extend an object, the object which you are extending is the superclass of a newly
extended object.
Instance: Whenever you create an object by calling its constructor, it will be called an instance.
To simplify this, whenever you write some thing like this $var = new Object(); you actually create
an instance of object class.
General Coding Conventions
We will be following some conventions in our codes throughout the book. Not being too strict,
these conventions will help you to maintain your application at a large extent. Also, it will
increase the maintainability of your code. It will also help you to write efficient code by avoiding
duplicity and redundant objects. Last but not least, it will make your code much more readable.

In a single php file, we never write more than one class at a time. Out of the scope of that
class, we will not write any procedural code.
We will save any class with a proper naming convention. For example we will save the
file where we place the Emailer class introduced earlier in this chapter as class.emailer.php.
What benefits can you achieve using this naming convention? Well, without going inside
that file, you are now at least confirmed that this file contains a class named "Emailer".
Never mix the case in filenames. It creates ugly application structure. Go ahead with all
small letters.
Like classes, we will save any interface as interface.name.php, Abstract class as
abstract.name.php, and Final class as final.name.php.
We will always use Camel case while naming our classes. And that means the first letters
of the major part is always a capital letter and the rest are small letter. For example a
class named "arrayobject" will be more readable if we write ArrayObject.
While writing the name of properties or class variables, we will follow the same
convention.
While writing the name of a method, we will start with a small letter and then the rest
are camel case. For example, a method to send an email could be named as sendEmail.

Modifiers
Private: Properties or methods declared as private are not allowed to be called from outside the class.
However any method inside the same class can access them without a problem. In our Emailer class we
have all these properties declared as private, so if we execute the following code we will find an error.
Public: Any property or method which is not explicitly declared as private or protected is a
public method. You can access a public method from inside or outside the class.
Protected: This is another modifier which has a special meaning in OOP. If any property or
method is declared as protected, you can only access the method from its subclass.

A constructor method is the method that executes automatically while creating instances of the class. In
PHP5, there are two ways you can write a constructor method inside a class. The first one is to create a
method with the name __construct() inside the class. The second is to create a method naming exactly the
same as class name.

Now, you may ask if a class can have constructors in both styles? This means a function named
__construct() and a function named the same as class name. So which constructor will execute, or will they
both execute? This is a good question. Actually there is no chance of executing both. If there is a
constructor in both styles, PHP5 will give preference to the __construct() function and the other one will
be ignored.

A destructor is called when the object is destructed or the script is stopped or exited.

If you create a __destruct() function, PHP will automatically call this function at the end of the script.

Notice that the destruct function starts with two underscores (__)!

Don't forget that just because PHP will close resources on termination for you doesn't
mean that it's bad to explictly close them when you no longer need them (or good to
not close them)... It depends on the use case (is it being used right up to the end, or is
there one call early on and then not needed again for the rest of execution)...

Now, we know that __destruct is called when the object is destroyed. Logically, what
happens if the object is destroyed? Well, it means it's no longer available. So if it has
resources open, doesn't it make sense to close those resources as it's being destroyed?
Sure, in the average web page, the page is going to terminate shortly after, so letting
PHP close them usually isn't terrible. However, what happens if for some reason the
script is long-running? Then you have a resource leak. So why not just close everything
when you no longer need it (or considering the scope of the destructor, when it's no
longer available)?

You might also like