<a href="http://www.onlinejobsfree.com/?id=1161537"><img src="http://www.onlinejobsfree.com/mem/images/300-
Friday, 30 November 2012
Monday, 19 November 2012
Differences between PHP4 and PHP5
Differences between PHP4 and PHP5:
Here's a quick overview of what has changed between PH4 and PHP5. PHP5 for the most part is backwards compatible with PHP4, but there are a couple key changes that might break your PHP4 script in a PHP5 environment. If you aren't already, I stronly suggest you start developing for PHP5. Many hosts these days offer a PHP5 environment, or a dual PHP4/PHP5 setup so you should be fine on that end. Using all of these new features is worth even a moderate amount of trouble you might go through finding a new host!
Note: Some of the features listed below are only in PHP5.2 and above. Object ModelThe new OOP features in PHP5 is probably the one thing that everyone knows for sure about. Out of all the new features, these are the ones that are talked about most! Passed by ReferenceThis is an important change. In PHP4, everything was passed by value, including objects. This has changed in PHP5 -- all objects are now passed by reference.
PHP Code:
$joe = new Person();$joe->sex = 'male';Note that this also means you can stop using the reference operator (&). It was common practice to pass your objects around using the & operator to get around the annoying pass-by-value functionality in PHP4. Class Constants and Static Methods/Properties You can now create class constants that act much the same was as define()'ed constants, but are contained within a class definition and accessed with the :: operator. Static methods and properties are also available. When you declare a class member as static, then it makes that member accessible (through the :: operator) without an instance. (Note this means within methods, the $this variable is not available) Visibility Class methods and properties now have visibility. PHP has 3 levels of visibility:
In PHP5, all constructors are named __construct(). That is, the word construct prefixed by two underscores. Other then this name change, a constructor works the same way. Also, the newly added __destruct() (destruct prefixed by two underscores) allows you to write code that will be executed when the object is destroyed. Abstract ClassesPHP5 lets you declare a class as abstract. An abstract class cannot itself be instantiated, it is purely used to define a model where other classes extend. You must declare a class abstract if it contains any abstract methods. Any methods marked as abstract must be defined within any classes that extend the class. Note that you can also include full method definitions within an abstract class along with any abstract methods. InterfacesPHP5 introduces interfaces to help you design common APIs. An interface defines the methods a class must implement. Note that all the methods defined in an interface must be public. An interface is not designed as a blueprint for classes, but just a way to standardize a common API. The one big advantage to using interfaces is that a class can implement any number of them. You can still only extend on parent class, but you can implement an unlimited number of interfaces. Magic Methods There are a number of "magic methods" that add an assortment to functionality to your classes. Note that PHP reserves the naming of methods prefixed with a double-underscore. Never name any of your methods with this naming scheme! Some magic methods to take note of are __call, __get, __set and __toString. These are the ones I find most useful. Finality You can now use the final keyword to indicate that a method cannot be overridden by a child. You can also declare an entire class as final which prevents it from having any children at all. The __autoload FunctionUsing a specially named function, __autoload (there's that double-underscore again!), you can automatically load object files when PHP encounters a class that hasn't been defined yet. Instead of large chunks of include's at the top of your scripts, you can define a simple autoload function to include them automatically.
PHP Code:
function __autoload($class_name) {Standard PHP LibraryPHP now includes a bunch of functionality to solve common problems in the so-named SPL. There's a lot of cool stuff in there, check it out! For example, we can finally create classes that can be accessed like arrays by implementing the ArrayAccess interface. If we implement the Iterator interface, we can even let our classes work in situations like the foreach construct. Miscellaneous Features Type Hinting PHP5 introduces limited type hinting. This means you can enforce what kind of variables are passed to functions or class methods. The drawback is that (at this time), it will only work for classes or arrays -- so no other scalar types like integers or strings. To add a type hint to a parameter, you specify the name of the class before the $. Beware that when you specify a class name, the type will be satisfied with all of its subclasses as well.
PHP Code:
function echo_user(User $user) {ExceptionsPHP finally introduces exceptions! An exception is basically an error. By using an exception however, you gain more control the simple trigger_error notices we were stuck with before. An exception is just an object. When an error occurs, you throw an exception. When an exception is thrown, the rest of the PHP code following will not be executed. When you are about to perform something "risky", surround your code with a try block. If an exception is thrown, then your following catch block is there to intercept the error and handle it accordingly. If there is no catch block, a fatal error occurs.
PHP Code:
try {There is a new error level defined as E_STRICT (value 2048). It is not included in E_ALL, if you wish to use this new level you must specify it explicitly. E_STRICT will notify you when you use depreciated code. I suggest you enable this level so you can always stay on top of things. Foreach Construct and By-Reference Value The foreach construct now lets you define the 'value' as a reference instead of a copy. Though I would suggest against using this feature, as it can cause some problems if you aren't careful:
PHP Code:
foreach($array as $k => &$v) {New Functions PHP5 introduces a slew of new functions. You can get a list of them from the PHP Manual. New Extensions PHP5 also introduces new default extensions.
__________________
|
PHP OOPs concepts
In the earlier tutorials we have witnessed keywords like public, private and protected. These are nothing but access specifiers. So, lets understand what access specifiers are.
Definition of Access Specifiers
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.
Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members. Access specifiers can either be public, private or protected.
Why do we need Access specifiers
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.
Access specifiers are used as a key component of Encapsulation and Data Hiding. By using either of the access specifiers mentioned above i.e. public, private or protected you can hide or show the internals of your class to the outside world.
Explanation of each access specifier
1. Private
2. Protected
3. Public
2. Protected
3. Public
1. Private
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:
A private access specifier is used to hide the data member or member function to the outside world. This means that only the class that defines such data member and member functions have access them. Look at the example below:
class Customer { private $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $c = new Customer(); $c->setName("Sunil Bhatia"); echo $c->name; //error, $name cannot be accessed from outside the class //$name can only be accessed from within the class echo $c->getName(); //this works, as the methods of the class have access //to the private data members or methods
In the above example, echo $c->name will give you an error as $name in class Customer has been declared private and hence only be accessed by its member functions internally. Therefore, the following line echo $c->getName() will display the name.
2. Public
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:
A public access specifier provides the least protection to the internal data members and member functions. A public access specifier allows the outside world to access/modify the data members directly unlike the private access specifier. Look at the example below:
class Customer { public $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } $c = new Customer(); $c->setName("Sunil Bhatia"); echo $c->name; // this will work as it is public. $c->name = "New Name" ; // this does not give an error.
In the above example, echo $c->name will work as it has been declared as public and hence can be accessed by class member functions and the rest of the script.
3. Protected
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:
A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world). Look at the example below:
class Customer { protected $name; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } } class DiscountCustomer extends Customer { private $discount; public function setData($name, $discount) { $this->name = $name; //this is storing $name to the Customer //class $name variable. This works // as it is a protected variable $this->discount = $discount; } } $dc = new DiscountCustomer(); $dc->setData("Sunil Bhatia",10); echo $dc->name; // this does not work as $name is protected and hence // only available in Customer and DiscountCustomer class
In the above example, echo $dc->name will not work work $name has been defined as a protected variable and hence it is only available in Customer and DiscountCustomer class.
You will learn more about inheritance later in this tutorial series. You should revisit this tutorial and read more on the protected section again when you understand inheritance better.
Important Note of Access Specifier in PHP5
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.
In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.
Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below
Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)
- PHP5 Tutorial – Learn to create a PHP5 Class
- PHP5 Tutorial – Learn to Create a PHP5 Class Object
- PHP5 Tutorial – Defining Attributes of a PHP5 Class
- PHP5 Tutorial – Defining Methods of a PHP5 Class
- PHP5 Tutorial – Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
- PHP5 Tutorial – Magic Methods – __toString() method
- PHP5 Tutorial – Magic Methods – __get() and __set()
- PHP5 Tutorial – Magic Methods – __isset() and __unset()
- PHP5 Tutorial – Magic Methods – __call() method
- PHP5 Tutorial – Magic Methods – __autoload() method
- PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
- PHP5 Tutorial – Magic Methods – __clone() method
Thursday, 8 November 2012
Differences between wp_query and get_posts
query_posts()is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use ofpre_get_postshook, for this purpose. TL;DR don't use query_posts() ever;get_posts()is very similar in usage and accepts same arguments (with some nuances, like different defaults), but returns array of posts, doesn't modify global variables and is safe to use anywhere;WP_Queryclass powers both behind the scenes, but you can also create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere.
The basic difference is that
query_posts() is really only for modifying the current Loop. Once you're done it's necessary to reset the loop and send it on its merry way. This method is also a little easier to understand, simply because your "query" is basically a URL string that you pass to the function, like so:query_posts('meta_key=color&meta_value=blue');
On the other hand,
wp_query is more of a general purpose tool, and is more like directly writing MySQL queries than query_posts() is. You can also use it anywhere (not just in the Loop) and it doesn't interfere with any currently running post queries.
I tend to use
wp_query more often, as it happens. Really, it's going to come down to your specific case.
Subscribe to:
Comments (Atom)