Knowledge7

The Linux and Mobile Solution Provider

  • About
  • Training
  • Services
  • Clients
  • In the news
  • Blog
  • Contact Us
You are here: Home / Archives for Topic

Object-Oriented Principles

This topic is part of our Object-Oriented Programming in PHP training

Developing software using object-oriented principles require a programmer to have a sound understanding of a number of important concepts:

  • Object-Oriented Analysis: what is the problem to be solved?
  • Object-Oriented Design: how are we going to solve the problem
  • Object-Oriented Programming: actually implementing the solution using an Object-Oriented Programming Language

The best way to learn Object-Oriented Analysis and Design is to read

where it is shown how to use the Unified Modeling Language (UML) and Design Patterns when developing software.

A Design Pattern is a solution which experienced software developers have been using over the years when they have to solve a given type of problem. The most popular book covering the fundamental Design Patterns is:

This topic is part of our Object-Oriented Programming in PHP training

Our forthcoming training courses

  • No training courses are scheduled.

Accessing MySQL with PHP

This topic is part of our Web Development with PHP and MySQL training

PHP can access MySQL databases using a number of libraries. Two of them are mysqli and PDO, both part of standard PHP. mysqli provides functions to manipulate data in a MySQL database. PDO, as stated on the PHP website:

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server.

PDO provides a data-access abstraction layer, which means that, regardless of which database you’re using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn’t rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

PDO ships with PHP 5.1

The work to do is to modify the web application being written to make it save orders to a MySQL database instead of in a text file. The first thing to do is to decide upon a schema (i.e. tables and columns). The application will use PDO.

Prepared statements

In order to increase performance and security, it is advisable to use prepared statements when this is possible.

The work to do is to add to the web application a second report. This report should give, for each customer, the amount of phones bought and the cost. In case, the amount is more than 20, that customer should get 20% for this phone. If the number of phones is between 10 and 19, the customer should get 10% off.

This topic is part of our Web Development with PHP and MySQL training

Our forthcoming training courses

  • No training courses are scheduled.

Parsing JSON and XML documents

This topic is part of our Web Development with PHP and MySQL training

Flickr has a public API which can return a JSON response containing a list of photos on Flickr tagged with a specific keyword.

The work to do is to add to the web application a feature where one can see recent photos on Flickr tagged with the name of the mobile phone being examined. For example, when someone is looking at the Nokia E75, he/she should be able to see Flickr photos tagged with nokiae75.

RSS (which is XML) is also very common on the web. For instance, all blogs have a RSS feed including Nokia’s where the latest news on Nokia mobile phones can be easily read.

The work to do is to add these latest news on the homepage on the web application. For that, the RSS feed needs to be read from the Nokia’s blog, parsed as an XML file and the appropriate HMTL generated.

This topic is part of our Web Development with PHP and MySQL training

Our forthcoming training courses

  • No training courses are scheduled.

Using filters and templates in PHP

This topic is part of our Web Development with PHP and MySQL training

An order form can be added to the website created before in order to transform it into an e-commerce website. When someone orders a phone, the order should be saved to a text file. A reporting facility can also be added.

Filters were recently added to PHP in order to provide sanitization as well as validation facilities. The work to do is to make sure that any data being submitted from the order form is properly sanitized and validated. This helps in making the web application more secure.

Usual PHP scripts mix both programming elements (the PHP) with HTML markup. This tends to make the web application less maintainable and can prevent web designers and programmers from working at the same time.

One common solution to that problem is to use a templating engine like Smarty. This engine allows the programmer to separate PHP from HTML markup while increasing flexibility. The work to do is to rewrite the web application to use Smarty.

This topic is part of our Web Development with PHP and MySQL training

Our forthcoming training courses

  • No training courses are scheduled.

Object-Oriented Programming

This topic is part of our Object-Oriented Programming in PHP training

When version 5.0 was released in 2004, PHP became a full-fledged Object-Oriented Programming Language (similar to Java and C#).

Exercises

We will implement a web application which manages people, dogs and cars in a number of steps until we have explored the most important object-oriented constructs found in PHP5:

  1. Implement the Person and Dog classes (+ required constructors, methods, attributes) and instantiate one object from each class.
  2. Add visibility constructs (private for attributes / public for methods).
  3. Add the Car class with its association as shown in the above diagram. Given that PHP is dynamically typed, what is the risk here?
  4. Use type hints to prevent type mismatch.
  5. Implement the Nameable interface as well as the Nameables class (use an array to hold references). Implement getNames() using foreach(). Use type hints to prevent type mismatch.
  6. Use an exceptions for error handling when someone attempts to create a Car with no number.
  7. Replace the exception by a custom exception.
  8. Use inheritance (sparingly) to create a specialized version of Person called Doctor with a special behaviour for getName().
  9. Replace the array in Nameables by a SplDoublyLinkedList of the Standard PHP library.
  10. Use mktime to make sure that each Car created has a registration date of 25 December 2010 at 10:00
  11. Use strtotime to make sure that each Car created has a first servicing date which is next Saturday at 10:00.
  12. Use strtotime to make sure that each Car created has a second servicing date which is exactly three months after the first one.
  13. Use a static attribute which are shared among all Cars to keep a count of all Cars which have been instantiated.
This topic is part of our Object-Oriented Programming in PHP training

Our forthcoming training courses

  • No training courses are scheduled.

Design Patterns

This topic is part of our Object-Oriented Programming in PHP training

A Design Pattern is a solution which experienced software developers have been using over the years when they have to solve a given type of problem. The most popular book covering the fundamental Design Patterns is:

Useful design patterns include:

  • creational patterns like Abstract factory and Singleton,
  • structural patterns like Adapter (or Façade) and Composite and
  • behavioral patterns like Observer and Strategy

Exercises

We will implement an event database using our knowledge of object-oriented programming and design patterns.

The domain logic of the web application is:

Implement the following:

  1. User domainlogic class with its unit test
  2. Event domainlogic class with its unit test
  3. Repository domainlogic class with its unit test (together with helper classes if needed)

The persistence layer of the application is based on the Strategy design pattern. Implement:

  1. The Database and the Engine interface (its concrete classes will be implemented later…)
  2. InMemoryEngine which manages data in memory.
  3. Create a main menu as well as functionality to show all events
  4. Show events for a specific period
  5. Show events by keyword
  6. Show events falling on a specific date
  7. Create a new event (not really useful because we are using an in memory database…)
  8. Consequently, replace the in memory database engine by a MySQL database engine.
This topic is part of our Object-Oriented Programming in PHP training

Our forthcoming training courses

  • No training courses are scheduled.

Using a CSS Framework

This topic is part of our Web Development with PHP and MySQL training

A lot of web designers believe that it is a waste of time to write CSS from scratch. It is better to use an existing CSS framework and build upon that.

One good framework is Blueprint. It provides a flexible grid on which the presentation of a website can be based. Consequently, it is easy to experiment with different layouts. Of course, Blueprint is compatible with all major browsers.

The work to do is to replace the most of the “hand written” CSS in the previous example with Blueprint to increase maintainability.

This topic is part of our Web Development with PHP and MySQL training

Our forthcoming training courses

  • No training courses are scheduled.

Developing semantic W3C compliant web pages

This topic is part of our Web Development with PHP and MySQL training

People use HTML and CSS to create web pages. Over the years, a number of good design principles have been identified and, when followed, allow anyone to produce websites which are standards-compliant, compatible with all the major websites yet flexible and easily maintainable.

The principles are:

  • Build websites using semantic HTML. Previously, HTML pages mixed structure and presentation. Now, people use CSS for presentation which implies that HTML should only contain the structure of a document. Semantic HTML goes further, in the sense that it uses markup to reinforce the semantics (i.e. the meaning) of a document. For example, only one H1 should be used in a document as one document cannot possibly have more than one main title.
  • Use CSS for formatting. Typographic attributes (e.g. typefaces, weight, etc.) as well as colours, margins, backgrounds, etc. should always be specified in a CSS stylesheet which is linked from the HTML file.
  • Use CSS for positioning. For many years, people used HTML tables for positioning objects on an HTML page but this was brittle and tended to produce bloated webpages.

The work to do is to progressively create a website like the one pictured above using semantic HTML first, then writing CSS “manually” both for formatting and positioning.

Note that web standards are in constant evolution. For example, there is a lot of buzz around HTML5 and CSS3 right now even though the standardisation process is still in progress.

This topic is part of our Web Development with PHP and MySQL training

Our forthcoming training courses

  • No training courses are scheduled.

PHP and the Web

This topic is part of our Object-Oriented Programming in PHP training

Linux, Apache, MySQL and PHP are collectively known as the LAMP software bundle and power most of the websites of the planet.

Linux is the most popular operating system on the Internet today. It’s powerful, reliable and, most of the time, free. Linux powers all of Google’s as well as Amazon’s infrastructure for instance. Apache is the most popular web server used. According to Netcraft, Apache powers 60% of all websites in January 2011. MySQL is the most popular relational database used in the world.

And, finally, PHP is also very popular. WordPress, Drupal and Joomla are written in PHP and, of course, Wikipedia and Facebook are too.

In other words, PHP (together with Linux, Apache and MySQL) power a large part of the Internet.

Exercises

We wish to automate the buying of auto parts online. We will proceed in steps until we get a reasonable web application. Write simple PHP scripts to do the following:

  1. Display static text (typically, “HelloWorld”) when a form is submitted
  2. Display dynamic text (using the date() function) when a form is submitted
  3. Accessing form variables using superglobals $_GET, $_POST and/or $_REQUEST
  4. Display form variables using string interpolation
  5. Use HEREDOCS
  6. Use variables (integer, float, string, boolean, array, object), constants, operators (string, arithmetic, comparison, logical, error suppression @, execution `) to do calculations
  7. Use numberformat to format currencies appropriately
  8. Use if statements to test the existence of form variables and to make the script work gracefully if form variables do not exist or contain non valid data
  9. Use elseif to offer discounts depending on quantity of products ordered
  10. Use switch to display appropriate messages depending on how the customer has discovered the website
  11. Use loops (i.e. while and/or for) to display shipping rates which depend on distance travelled
  12. Store orders in a file and write a script to display all past orders
  13. Use arrays to summarise all past orders
  14. Use foreach and associative arrays to display help pages on various topics (shipping, warranty, contact)
  15. Use functions and array_walk to simplify the display of help pages
This topic is part of our Object-Oriented Programming in PHP training

Our forthcoming training courses

  • No training courses are scheduled.

The PHP Programming Language

This topic is part of our Object-Oriented Programming in PHP training

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

  • Official website for PHP
  • PHP on Wikipedia

PHP is an imperative programming language belonging to the C family and, as such, follows the C syntax, has control structures such as the if statement and loops (while, for, foreach etc). PHP also has variables, functions and supports object-oriented programming.

This topic is part of our Object-Oriented Programming in PHP training

Our forthcoming training courses

  • No training courses are scheduled.
« Previous Page

Looking for something?

Want to know more?

Get our newsletter

Discover the latest news, tips and tricks on Linux, the Web and Mobile technologies every week for FREE

This work is licensed by Knowledge7 under an Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.