Home / Articles / Programming languages / Scripting languages / PHP / Classes and objects in PHP

Classes and objects in PHP

Classes and objects in PHP
 
Classes and objects in PHP
  • Currently 4 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 4/5 (1 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


Class - is a basic concept in object-oriented programming (OOP). Classes form syntactic base of OOP. They can be seen as a kind of "container" for logically related data and functions (usually called methods - see below). Simply said, class - a kind of data type.

An instance of a class is an object. Object is a collection of data (properties) and functions (methods) which process them. Properties and methods are called class members. In general, an object is anything that supports encapsulation.

If a class can be considered of as a type of data, then an object can be considered of as a variable (by analogy). Script can work with multiple objects of the same class, as well as with multiple variables.

Within an object, data and the code (class members) can be either open or closed. Open data and class members are accessible to other parts of the program that are not part of the object. But closed data and class members are accessible only within that object.

In PHP to define classes syntax word class is used:

class Nameofclass {
  //defining class members - properties and methods for their processing
}

Use operator new to declare an object:

Object = new Nameofclass;

The data is defined by the function word var. The method is defined in the same way as a regular user-defined function. It's also possible to pass parameters to method.

Let's sum it up: class declaration must start with the keyword class (just as the function declaration starts with the keyword function). Each declaration of a property contained in a class must be preceded by the keyword var. Properties can relate to any data type supported by PHP, you can think of them as variables with small differences. First you declare the properties then the methods that are very similar to the typical user-defined functions.

According to generally accepted rules of OOP class names start with capital letters, and all the words in the name of the method, except the first one start with capital letters (the first word begins with a lowercase letter). Of course you can use any other way to denote these objects that you find convenient, the main thing - choose a standard and stick to it.

Example of a class in PHP:

// Creating a new class Coordinates:
class Coordinates {
  // data (properties):
  var $name;
  var $address;

  // methods:
  function Name() {
    echo "<h1>Justine</h1>";
  }
}

// Creating a new object with class Coordinates:
$object = new Coordinates;

 

Read also

Initialization of objects in PHP

Initialization of objects in PHP

What is PHP?

What is PHP?

Post comment

basicuse.net
html5_css
106160485398655174790

Quick navigation

General navigation