all* abt PHP OO :P *conditions apply :D

Hi Folks spent a day with PHP in action Book
An awesome book for developers :)
Wanna share some useful tips out of it
0. When ISA (Inheritance)? When HASA (Composition)?
Only way to decide it is relationship if it is going to be 1:1 go for ISA 1:Many go for HASA :) Cool but true :)

1. Predict what would happen
$object1 = new base();
$object2 = $object1;
in PHP 4 a photocopy of present appearance of the object1 is copied to object2, any changes in object1 later ll not affect the latter :D
in PHP 5 it means shallow copy :) jus the reference is returned to object2 so changes on former or latter ll reflect on latter or former too :D
wanna do the PHP 5 way in PHP 4 then do
$object2 = &$object1; Explicit way :)
But this has the difference with the PHP5 way, in this jus the symbol table is adjusted (Clever no) :)

2. Can abstract functions be private?
Don't scold me if u know the meaning of abstract class :D
The answer is NOOOOOOOOO obviously :) Because abstract methods are need to be implemented by the class inherits it :) so it can't be private
So, what abt protected? It can be :D try it


3. FACT: A class with abstract method should be declared abstract :)
4. What would happen if the class inheriting an abstract class doesn't implement all the abstract methods?
Nothing the compiler will knock ur screen saying that declare the class that derives abstract :) else implement the rest :)


5. FACT: Interface is an abstract class with all methods declared abstract :)
6. Then why do we have both?
A class can inherit only one (abstract) class but can implement many interfaces :) This is how they implement multiple inheritance in java :)
Interfaces are pure abstract classes :P that is, all methods declared are abstract and public by default. So, no method defn can be there. But in abstract class there can be non-abstract methods(implemented with in the class itself) and private members are allowed too :)


7. FACT: Class is a virtual home for an object. The home will exist even if there is no habitants :D So U can loot static members and methods
8. FACT: Diff between die() and exception thrown with no catch is, stack trace is printed in the latter
9. FACT: Class constants won't accept complex expressions :) even string concatenation
10. FACT: PHP is a (statically-incomplete) dynamically typed language :D PHP 5 alows typehints :) function add(integer $a,integer $b) is valid, in the book they have specified that compiler ll generate a warning but im getting a fatal error :D
11. FACT: Method overloading is done through __CALL and by the way this is not same as function overloading :) this is a life saver at the verge of death of execution because of undefined methods :)
12. FACT: In the same way we have __autoload for classes :)

Read some design prinicples like
OCP (Open to extension and Closed to modification)
SRP (Single Responsiblity Policy) - Don't be good at many things :D
DIP (Dependency Inversion Principle) - Program for interface not for implementation :) Believe in abstraction :D

Comments

Popular posts from this blog

Headless Chrome/Firefox Selenium Java in Amazon EC2

Cassandra PHPCassa & Composite Types

Selenium Webdriver in Nodejs + Javascript