PHP
  1. PHP intro
  2. PHP basics
  3. PHP variables
  4. PHP functions
  5. PHP conditions
  6. PHP loops
  7. PHP arrays
  8. PHP classes & objects
  9. PHP strings
  10. PHP forms
  11. PHP entities
  12. PHP files
  13. PHP include files
  14. PHP date & time
  15. PHP cookies
  16. PHP databases
  17. PHP sessions
  18. PHP summary

PHP classes and objects

PHP is an object-oriented language. An object-oriented language is a language where data types of data structures are defined by the programmer as well as their properties and the things that can be done with them.

This tutorial focuses on:

What is a class?

A class is a template for a real world object to be used in a program. For example, a programmer can create a car class which emulates a car. This class can contain the properties of a car (color, model, year, etc.) and methods (functions) that specify what the car does (drive, reverse, stop, etc.).

Creating a class

To create a class, use the class keyword, followed by the class name, followed by an opening brace and a closing brace. All the specifics and logisitcs of the class go between the braces.

Syntax:
class className{ }
Example:
class Book{ }

Creating variables for a class

Class variables are known as properties, because they store important information in regards to the class - hence, they are the classes properties. We will be referring to class variables as 'variables' or 'properties' interchangeably throughout this lesson.

Example:
class Book{ var $title; var $numPages; }

The above code uses the Book class and creates two variables for it. The first is $title - a variable which will store the title of the book. The second is $numPages - a variable which will store the number of pages in the book.

NOTE: When declaring class variables, they must be declared with the var keyword. Variables that are not declared as class variables are not declared with the var keyword.

Creating functions for a class

Class functions are just like regular functions, except they are defined inside a class definition. Class functions are known as methods, and they can be used to either retrieve data or manipulate data. We will be referring to class functions as 'functions' or 'methods' interchangeably throughout this lesson.

Example:
class Book{ var $title; var $numPages; function setNumPages($numOfPages){ $numPages = $numOfPages; } function getNumPages(){ return $numPages; } function setTitle($Title){ $title = $Title; } function getTitle(){ return $title; } }

An explanation of each function from the above example:

Instantiating a class

So you have created your class with its variables and functions, and now what to do with it? Now you can create objects based on how you designed your class. The process of creating an object is called instantiation. You can create an object by creating a variable which stores the object, and using the new keyword and the name of the class to instantiate from.

Syntax:
$objectName = new nameOfClass();
Example:
$yellowPages = new Book();

Creating a class constructor

What if you wanted an object to automatically have a certain value when it is instantiated? For example, if you wanted all instances of the Book class to automatically have 200 pages? This is what class costructors are for. Class constructors are used to set objects to have certain values when they are initialized. The function used for this task is the __construct() function, and it goes inside the class definition.

Syntax:
function __construct($param1, $param2, etc.){ $this -> propertyName = $value; }
Example:
class Book{ $title; $numPages; function __construct(){ $this->$numPages = 200; } }

Did you notice a new keyword? The keyword this is a special keyword used when creating classes. The purpose of it is to refer to the current class. The code inside the __construct function from the example above could have been $Book->$numPages = 200; instead of $this->$numPages = 200;, and the result would have been the same. When creating classes, $this can be used synonomously with the name of the class (in this case the class being the Book class).

In the above example, the __construct() function specifies that all instances of the Book class will automatically have 200 pages.

Referencing the variables of a class with an object

You can reference the variables of a class with an object by referring to them by name.

Syntax:
$object->varName;
Example:
$yellowPages->numPages;

This example would reference the $numPages variable of the $yellowBook class. Notice how in the reference to the variable there is no $ symbol in front of the variable name. When referencing a class variable with the object name, you do NOT have to use the $ symbol.

Using the functions of a class with an object

Just as with the variables of a class, you can use the functions of a class with an object by referring to them by name.

Syntax:
$object->functionName();
Example:
$yellowPages->getNumPages();

This example would call the getNumPages() function of the Book class.

© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information