Saturday, October 29, 2011

Difference between PHP 4 and 5 - 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.

Difference between PHP 4 and 5 - __autoload Function

Using a specially named function, __autoload, 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.

function __autoload($class_name) {
require_once "./includes/classes/$";

Note you can change the autoload function or even add multiple autoload functions using spl_autoload_register and related functions.

Difference between PHP 4 and 5-New Extensions

PHP5 also introduces new default extensions.

1. SimpleXML for easy processing of XML data
2. DOM and XSL extensions are available for a much improved XML-consuming experience. A breath of fresh air after using DOMXML for PHP4!
3. PDO for working with databases. An excellent OO interface for interacting with your database.
4. Hash gives you access to a ton of hash functions if you need more then the usual md5 or sha1.

Difference between PHP 4 and 5 - Exceptions

PHP 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.

try {
} catch (AccessDeniedException $e) {
die('Could not write the cache, access denied.');
} catch (Exception $e) {
die('An unknown error occurred: ' . $e->getMessage());

Monday, October 17, 2011

Difference between PHP 4 and 5 - passed by reference

Passed by Reference

This 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.

$joe = new Person();
$joe->sex = 'male';

$betty = $joe;
$betty->sex = 'female';

echo $joe->sex; // Will be 'female'

The above code fragment was common in PHP4. If you needed to duplicate an object, you simply copied it by assigning it to another variable. But in PHP5 you must use the new clone keyword.

Sunday, October 2, 2011

what is MVC? why its been used?

Model-view-controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model.

1 Modular separation of function
2 Easier to maintain
3 View-Controller separation means:
A — Tweaking design (HTML) without altering code B — Web design staff can modify UI without understanding code.

what is garbage collection? default time ? refresh time?

Garbage Collection is an automated part of PHP , If the Garbage Collection process runs, it then analyzes any files in the /tmp for any session files that have not been accessed in a certain amount of time and physically deletes them. Garbage Collection process only runs in the default session save directory, which is /tmp. If you opt to save your sessions in a different directory, the Garbage Collection process will ignore it. the Garbage Collection process does not differentiate between which sessions belong to whom when run. This is especially important note on shared web servers. If the process is run, it deletes ALL files that have not been accessed in the directory. There are 3 PHP.ini variables, which deal with the garbage collector: PHP ini value name default session.gc_maxlifetime 1440 seconds or 24 minutes session.gc_probability 1 session.gc_divisor 100

What are the advantages and disadvantages of Cascading Style Sheets?

External Style Sheets:
Can control styles for multiple documents at once. Classes can be created for use on multiple HTML element types in many documents. Selector and grouping methods can be used to apply styles under complex contexts.

An extra download is required to import style information for each document The rendering of the document may be delayed until the external style sheet is loaded Becomes slightly unwieldy for small quantities of style definitions.

Embedded Style Sheets

Classes can be created for use on multiple tag types in the document. Selector and grouping methods can be used to apply styles under complex contexts. No additional downloads necessary to receive style information.

This method can not control styles for multiple documents at once

Inline Styles

Useful for small quantities of style definitions. Can override other style specification methods at the local level so only exceptions need to be listed in conjunction with other style methods.

Does not distance style information from content (a main goal of SGML/HTML). Can not control styles for multiple documents at once. Author can not create or control classes of elements to control multiple element types within the document. Selector grouping methods can not be used to create complex element addressing scenarios.