PHP 5: Introduction to classes

PHP 5: Introduction to classes

Classes are used as an abstraction of a real world situation. E.g

class User
{
}

may be used to contain the code that will represent a user of an application.

Objects have properties and methods. Properties may be thought of as adjectives (e.g. coat->blue) and methods as verbs (e.g. coat->putOn()).

e.g. to create a user class, instantiate it and set a login property to true

class User
{
 var $isLoggedIn = false;
 //Variable to contain the logged-in
 //state, with a default value of ‘false’

 function User()
 {
 }

 function login($loginValue)
 {
  $this->isLoggedIn = true;
 }

}
$user = new User();
$user->login(true);

will create the user with a default property (isLoggedIn) and then log the user in with a method (login).

Next section: By reference or by value

Read more at:

PHP 5 Classes and objects on php.net

Leave a Reply