php object oriented notes

Object oriented programming gives a developer a better best approach to reuse code and faster development ; there are given below brief concepts of oop.

Object-orientation has become a key concept behind proper PHP software design. This book follows
the idea that in properly designed software, all business logic (the rules that drive how an
application behaves) should be object oriented. The only exception is when small scripts act as a
view or a way to display data returned from other objects.
Taking this approach solves a few problems because it:

  1. Allows for type hinting, which gives greater control over what variables are passed into
  2. Makes it easy to extend the functionality of existing code.
  3. Allows for established design patterns to be used to solve common software design problems
    and makes debugging much easier.


Instantiation and Polymorphism

The two key benefits of object-oriented programming in PHP are the ability to abstract data into
classes and for the application to act on those structures. It is important to understand polymorphism,
which is when one object appears and can be used in the same way as another object of
a related type. It stands to reason that if B is a descendant of A and a function can accept A as a
parameter, it can also accept B.

Older PHP applications will not define a visibility for the member methods. When the visibility is
not defi ned it is assumed to be public. A member variable or method (function inside a class) can
have one of three visibilities:

Difference between public,private and protected in php:

  1. private indicates that a member can be accessed only from within the class in which it is
    defined. Private members cannot be overridden in later classes because those classes too do
    not have access to the member.
  2. protected indicates that the member can be accessed only by the class in which it is defi ned
    and all descending classes.
  3. public indicates that the member is accessible globally across all of PHP.


Difference between static,abstract and final:


  1. Using static members are not tied to particular instances of a class and can be accessed by any
    instance. They should be used sparingly but are very useful for shared variables across all
    instances. The static keyword can also be used inside methods and functions to define a
    variable that is global to all calls to that function.
  2. Using final methods can never be redefined in descending classes and therefore their functionality
    cannot be changed.
  3. Using abstract methods must be implemented in all classes that descend from that class that
    defines it. Abstract methods can only be defined in classes that are marked as abstract. It is
    not possible to directly instantiate an abstract class because of the nature of abstraction.
  4. Using constant variables inside a class can also be declared constant.
  5. Constant in php always capitalized and static and their value can never be changed at run time. Unlike normal variables, constants cannot have a dollar sign in front of them .
  6. Constants are always public.
PHP Tutorials