K!

by Karl Bunyan

Programming, PHP, JavaScript, motorbikes, pubs, poker, football, news, restaurants and anything else

Saturday, December 11, 2004

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

Link to this post

Comments:
Post a Comment

Recent posts in the Programming category

Recent posts in the PHP category

Change the background image