let's talk about technology.
Learn about technologies that elevate your design and development, tools that make your job easier, and trends that you need to watch from all corners.

OOPhp

OOPhp

PHP has come a long way in becoming one of the best scripting languages online. Similar to most techno enthusiasts, i tend to dive into coding prior to taking notes of what the technology offers, and i missed out on the OO part of PHP for some time. I found this information useful to begin with - that is if you already have the basic knowledge of OO design and PHP.

Inheritance – It is not possible to extend multiple classes; a class can only inherit from one base class.

Static – To access a static property, we can use self var inside class like echo self::$foo;

Properties -Any property inside a class can has initial value but that should be static only, no expression.

Class Constant -The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call. Can be accessible like static properties

Autoload - Prior to 5.3.0, exceptions thrown in the __autoload function could not be caught in the catch block and would result in a fatal error. It works for interface too. From 5.3.0+ exceptions thrown in the __autoload function can be caught in the catch block, with 1 proviso. If throwing a custom exception, then the custom exception class must be available.

Constructor -Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

Overriding rules :
1. In the overriding, the method names and arguments (arg’s) must be same.
2. final methods can’t be overridden.

Strict Inheritance -When you define a class with abstract, any attempt to instantiate a separate instance of it will result in a fatal error.

Paamayim Nekudotayim(in Hebrew):
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.

Class Name in a variable:
As of PHP 5.3.0, it’s possible to reference the class using a variable

Static -A property declared as static can not be accessed with an instantiated class object. The pseudo-variable $this is not available inside the method declared as static.

Interface – All methods declared in an interface must be public. Interface supports multiple inheritance, multiple implements if both interface dont have same function name.

Overloading – Overloading in PHP provides means to dynamically “create” properties and methods, that is magic methods such as __set, __get, __call, __callStatic

Iterator – Php has two new built in interfaces – iterator and IteratorAggregate, using these – its easy to iterate a object. Object Iteration implementing Iterator –

Patterns – Patterns are ways to describe best practices and good designs.
The Factory pattern allows for the instantiation of objects at runtime.

The Singleton pattern applies to situations in which there needs to be a single instance of a class.

Magic Methods – *PHP reserves all function names starting with __ as magical.
__construct -
In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
__destruct – The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
__call is triggered when invoking inaccessible methods in an object context.
__callStatic is triggered when invoking inaccessible methods in a static context.
__set() is run when writing data to inaccessible properties.
__get() is utilized for reading data from inaccessible properties.
__isset() is triggered by calling isset() or empty() on inaccessible properties.
__unset() is invoked when unset() is used on inaccessible properties.
__sleep() – serialize() checks if your class has a function with the magic name __sleep. If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn’t return anything then NULL is serialized and E_NOTICE is issued.
__wakeup – Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that the object may have.
The intended use of __wakeup is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

__toString method allows a class to decide how it will react when it is converted to a string.

__invoke method is called when a script tries to call an object as a function

__set_state – This static method is called for classes exported by var_export() since PHP 5.1.0.

Object Cloning – When an object is cloned, PHP 5 will perform a shallow copy of all of the object’s properties. Any properties that are references to other variables, will remain references.
Once the cloning is complete, if a __clone() method is defined, then the newly created object’s __clone() method will be called, to allow any necessary properties that need to be changed.

Type Hinting – Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays. However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call. Failing to satisfy the type hint results in a catchable fatal error. Type hinting also works with functions:

Late Static Binding – can be used to reference the called class in a context of static inheritance.

Object Serialization – It serialize only vars and classname, not any method. Also to unserialize that value – that class file of that object should be include first.

more example and documentation can be found on php.net

Edited By: Intrepid
Jun-10-10 15:07:05

In the interests of clarity, it seemed necessary to constantly remind myself to pay not the slightest attention to the elegance of the presentation; I adhered conscientiously to the rule of the brilliant theoretician, Ludwig Boltzmann, to leave elegance to tailors and shoemakers. Einstein

Intrepid
Dude
useravatar
Offline
20 Posts
User info in posts
Administrator has disabled public posting

Re: OOPhp

thank you Intrepid  those r nice tips. And anyone who wants to start php-OOP check lynda.com they have nice video tutorial.

los
useravatar
Offline
11 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
24
Total Polls:
0
Total Posts:
13
Dormant:
User Info:
 
Total Users:
2483
Newest User:
wstvic
Members Online:
0
Guests Online:
29

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked

Thank you for your donation.

Amount: