Wickham's HTML & CSS tutorial

Sitemap | Home | Search | PHP Introduction

Object Oriented PHP

View in Firefox, Safari, Opera and IE but IE6 often needs different solutions. In general IE7 and IE8 display like Firefox apart from the HTML5 and CSS3 features. The IE9 & above updates are mainly related to HTML 5 and CSS3 issues and display to a large extent like other main browsers. Google Chrome is based on the same WebKit engine as Safari.

Some of the examples are provided just to show problems, others show solutions that work in most major browsers. Use the example that suits you.
red icon problems   gold icon warning: works in some situations or some browsers or needs care to display as you wish   green icon OK in Firefox, Safari, Opera, Google Chrome and IE

The main purpose of this tutorial as a whole is to show how IE and Firefox differ, although PHP should not affect the differences.

Some basic Object Oriented PHP information

1 green icon Object Oriented PHP was introduced with PHP version 5, so before you use any of this coding method you should check whether your hosting service or your client's hosting service supports PHP version 5 or above.

The principle is to place sections of code in separate PHP files to be used as classes in your main PHP page files in a similar way that CSS files contain styles linked as ids or classes from a separate HTML file.

Assume that you want a list of names and addresses with their variables in a main PHP file, the variables will be linked to codes which refer to a separate class file. This would be the code in the main file, which first has the link to the class file at the top (it could be combined in the other PHP tag but it must be first) and then the variables and then the echo statements:-

which displays as:-

Club Secretary's name & address:-
David G
45 Main Street
Club Members' names & addresses:-
Tom F and Liz F
37 Park Road

Note that the address of Liz F is the same as for Tom F so her address is echo $lizad -> get_tomaddress(); which can be useful if many persons work at the same company address.

The new code introduced in PHP 5 is -> which is used here with get_name( ).

The separate file class_library.php has this code:-

First set the variables; then the function get_name( ) which tells PHP be be ready to do something with the data and then return $this -> name( ); which allows PHP to use it. There is no semicolon ; after function which uses { } to enclose the return statement which is like echo and does have a semicolon ; at the end.

It may seem a lot of work setting up a class PHP file but with long and complicated PHP pages it should help keep information common to many pages isolated so that the main page code is shorter.

One of the main objectives when using Object Oriented PHP is to keep the designer code separate from business logic, meaning that when associated with a database it's often possible to have one object for the table or other HTML layout and another object to handle the database information. Do not have HTML or View code inside the class file.


View/Source or View/Page Source in browser menu to see all html code.

The body of this page has margin: 20px. The examples above are in a containing div with width: 730px; and margin: auto; so that the page centralises at large screen resolutions.

A lot of codes have been put in html tags in my examples rather than in a stylesheet or in a style in the head. I have done this for the convenience of the viewer so that most (but not all) codes are in one place and the stylesheet does not always have to be viewed in addition. When coding your own page you should try to put as much as possible in a stylesheet and link with id or class to the html tag.

Remember that when a Doctype is included above the head before the html tag (as it should be) then the overall width of a div is its defined width plus borders, margins and padding widths.

If there are differences between Firefox and IE6 that cannot be overcome with one code, code first to get a satisfactory solution in Firefox then create an IF style which will only apply to IE6:-
for instance, if margin-top: 20px; in Firefox needs to be margin-top: 30px; in IE6 then put the following in the head of the html/xhtml page:-
<!--[if ie 6]> <style type="text/css"> div { margin-top: 30px; } </style> <![endif]-->
or if there are many different styles, create a separate stylesheet:-
<!--[if ie 6]> <link rel="stylesheet" href="ie6.css" type="text/css"> <![endif]-->
IE6 will contain just the amended styles such as div { margin-top: 30px; } and others (no head or body tags or Doctype).

When looking at a page source for this site you may see code like &lt;p>Little Egret&lt;/p> instead of <p>Little Egret</p>. The code &lt; is because in that instance the code is to be displayed on the screen as text. If the < symbol was placed in the code a browser would activate the code and it would not display as text. Such code is not normally required when writing html code tags which are to be activated.

© Wickham 2009

top | Sitemap | PHP Introduction


web www.wickham43.net/