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:
- Allows for type hinting, which gives greater control over what variables are passed into
- Makes it easy to extend the functionality of existing code.
- 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:
- 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.
- protected indicates that the member can be accessed only by the class in which it is defi ned
and all descending classes.
- public indicates that the member is accessible globally across all of PHP.
Difference between static,abstract and final:
- 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.
- Using final methods can never be redefined in descending classes and therefore their functionality
cannot be changed.
- 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.
- Using constant variables inside a class can also be declared constant.
- 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 .
- Constants are always public.