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

Advertisements
This entry was posted in PHP, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s