PHP Tutorials
121. while loops in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: while loops are the simplest kind of loops. In the beginning of each iteration, the while's truth expression is evaluated. If it evaluates to true, the loop keeps on running and the statements inside it are executed. If it evaluates to false, the loop ends and the statement(s) inside the loop is skipped. For example, here's one possible implementation of factorial, using a while loop (assuming $n contains the number for which we want to calculate the factorial):
122. do...while Loops in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: The do...while loop is similar to the previous while loop, except that the truth expression is checked at the end of each iteration instead of at the beginning. This means that the loop always runs at least once.
123. Objects in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: The main difference in OOP as opposed to functional programming is that the data and code are bundled together into one entity, which is known as an object. Object-oriented applications are usually split up into a number of objects that interact with each other. Each object is usually an entity of the problem, which is self-contained and has a bunch of properties and methods. The properties are the object's data, which basically means the variables that belong to the object. The methods —if you are coming from a functional background —are basically the functions that the object supports. Going one step further, the functionality that is intended for other objects to be accessed and used during interaction is called an object's interface.
124. __toString() METHOD in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: Unlike most other data types, printing the object's id will usually not be interesting to you. Also, objects often refer to data that should have print semantics—for example, it might make sense that when you print an object of a class representing a person, the person's information would be printed out.
125. Interfaces in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: Class inheritance enables you to describe a parent-child relationship between classes. For example, you might have a base class Shape from which both Square and Circle derive. However, you might often want to add additional "interfaces" to classes, basically meaning additional contracts to which the class must adhere. This is achieved in C++ by using multiple inheritance and deriving from two classes. PHP chose interfaces as an alternative to multiple inheritance, which allows you to specify additional contracts a class must follow. An interface is declared similar to a class but only includes function prototypes (without implementation) and constants. Any class that "implements" this interface automatically has the interface's constants defined and, as the implementing class, needs to supply the function definitions for the interface's function prototypes that are all abstract methods (unless you declare the implementing class as abstract).
126. instanceof OPERATOR in PHP
By: Emiley J. : 2008-11-22
Description: The instanceof operator was added as syntactic sugar instead of the already existing is_a() built-in function (which is now deprecated). Unlike the latter, instanceof is used like a logical binary operator:
127. parent:: AND self:: in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: PHP supports two reserved class names that make it easier when writing OO applications. self:: refers to the current class and it is usually used to access static members, methods, and constants. parent:: refers to the parent class and it is most often used when wanting to call the parent constructor or methods. It may also be used to access members and constants. You should use parent:: as opposed to the parent's class name because it makes it easier to change your class hierarchy because you are not hard-coding the parent's class name.
128. Polymorphism in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: The subject of polymorphism is probably the most important in OOP. Using classes and inheritance makes it easy to describe a real-life situation as opposed to just a collection of functions and data. They also make it much easier to grow projects by reusing code mainly via inheritance. Also, to write robust and extensible code, you usually want to have as few as possible flowcontrol statements (such as if() statements). Polymorphism answers all these needs and more.
129. Static Methods in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: Similar to static properties, PHP supports declaring methods as static. What this means is that your static methods are part of the class and are not bound to any specific object instance and its properties. Therefore, $this isn't accessible in these methods, but the class itself is by using self to access it. Because static methods aren't bound to any specific object, you can call them without creating an object instance by using the class_name::method() syntax. You may also call them from an object instance using $this->method(), but $this won't be defined in the called method. For clarity, you should use self::method() instead of $this->method().
130. Static Properties in PHP
By: Andi, Stig and Derick : 2008-11-22
Description: Classes can declare properties. Each instance of the class (i.e., object) has its own copy of these properties. However, a class can also contain static properties. Unlike regular properties, these belong to the class itself and not to any instance of it. Therefore, they are often called class properties as opposed to object or instance properties. You can also think of static properties as global variables that sit inside a class but are accessible from anywhere via the class.