PHP Tutorials

131. public, protected, and private Methods in PHP

By: Andi, Stig and Derick : 2008-11-22

Description: Access modifiers may also be used in conjunction with object methods, and the rules are the same:


132. public, protected, and private Properties in PHP

By: Andi, Stig and Derick : 2008-11-22

Description: A key paradigm in OOP is encapsulation and access protection of object properties (also referred to as member variables). Most common OO languages have three main access restriction keywords: public, protected, and private. When defining a class member in the class definition, the developer needs to specify one of these three access modifiers before declaring the member itself. In case you are familiar with PHP 3 or 4's object model, all class members were defined with the var keyword, which is equivalent to public in PHP 5. var has been kept for backward compatibility, but it is deprecated, thus, you are encouraged to convert your scripts to the new keywords:


133. Destructors in PHP

By: Andi, Stig and Derick : 2008-11-22

Description: Destructor functions are the opposite of constructors. They are called when the object is being destroyed (for example, when there are no more references to the object). As PHP makes sure all resources are freed at the end of each request, the importance of destructors is limited. However, they can still be useful for performing certain actions, such as flushing a resource or logging information on object destruction. There are two situations where your destructor might be called: during your script's execution when all references to an object are destroyed, or when the end of the script is reached and PHP ends the request. The latter situation is delicate because you are relying on some objects that might already have had their destructors called and are not accessible anymore. So, use it with care, and don't rely on other objects in your destructors.


134. The new keyword and constructors in PHP

By: Andi, Stig and Derick : 2008-11-22

Description: Instances of classes are created using the new keyword. What happens during the new call is that a new object is allocated with its own copies of the properties defined in the class you requested, and then the constructor of the object is called in case one was defined. The constructor is a method named __construct(), which is automatically called by the new keyword after creating the object. It is usually used to automatically perform various initializations such as property initializations. Constructors can also accept arguments, in which case, when the new statement is written, you also need to send the constructor the function parameters in between the parentheses.


135. for Loops in PHP

By: Andi, Stig and Derick : 2008-11-22

Description: The start expression is evaluated only once when the loop is reached. Usually it is used to initialize the loop control variable. The truth expression is evaluated in the beginning of every loop iteration. If true, the statements inside the loop will be executed; if false, the loop ends. The increment expression is evaluated at the end of every iteration before the truth expression is evaluated. Usually, it is used to increment the loop control variable, but it can be used for any other purpose as well. Both break and continue behave the same way as they do with while loops. continue causes evaluation of the increment expression before it re-evaluates the truth expression.


136. Strings in PHP

By: Andi, Stig and Derick : 2008-11-21

Description: Strings in PHP are a sequence of characters that are always internally nullterminated. However, unlike some other languages, such as C, PHP does not rely on the terminating null to calculate a string's length, but remembers its length internally. This allows for easy handling of binary data in PHP - for example, creating an image on-the-fly and outputting it to the browser. The maximum length of strings varies according to the platform and C compiler, but you can expect it to support at least 2GB. Don't write programs that test this limit because you're likely to first reach your memory limit.


137. Integers and Floating-Point Numbers in PHP

By: Emiley J. : 2008-11-21

Description: Integers are whole numbers and are equivalent in range as your C compiler's long value. On many common machines, such as Intel Pentiums, that means a 32-bit signed integer with a range between –2,147,483,648 to +2,147,483,647. Integers can be written in decimal, hexadecimal (prefixed with 0x), and octal notation (prefixed with 0), and can include +/- signs.


138. superglobals in PHP

By: Emiley J. : 2008-11-21

Description: As a general rule, PHP does not support global variables (variables that can automatically be accessed from any scope). However, certain special internal variables behave like global variables similar to other languages. These variables are called superglobals and are predefined by PHP for you to use.


139. unset() and empty() functions in PHP

By: Emiley J. : 2008-11-21

Description: unset() "undeclares" a previously set variable, and frees any memory that was used by it if no other variable references its value. A call to isset() on a variable that has been unset() returns false. empty() may be used to check if a variable has not been declared or its value is false. This language construct is usually used to check if a form variable has not been sent or does not contain data. When checking a variable's truth value, its value is first converted to a Boolean according to the rules in the following section, and then it is checked for true/false.


140. isset() function in PHP

By: Emiley J. : 2008-11-21

Description: isset() determines whether a certain variable has already been declared by PHP. It returns a boolean value true if the variable has already been set, and false otherwise, or if the variable is set to the value NULL.