Introduction to Classes |
|
Fundamentals of Classes
Introduction
A class is a formula used to describe an object. The formula uses a list of items where each item describes a particular aspect. For example, a class used to describe a car for the motor vehicle office would have such characteristics as the tag number, the make, the model, the year, etc.
Creating a Class
To create a class, start with the following formula:
class Name { }
You must use the class keyword. This is followed by a required name. The rules of a class name are:
The suggestions for a class name are:
The name of a class is followed by its body, which is delimited by an opening and a closing curly brackets. The positions of the curly brackets are not important as long as they are created in the right order. Here is an example:
class House{}
The area inside the curly brackets is referred to as the body of the class:
<?php
class House
{
// The body of the class
}
?>
Introduction to the Members of a Class
In the body of the class, you can create the list of its characteristics, each as a member. A class can have various types of members. The most fundamental members of a class appear as regular variables. As seen for variables in previous lessons, this is created with a name preceded with $ and a value assigned to the variable. An example would be:
<?php
class House
{
$beds = 12;
}
?>
Creating a new Object
A class provides only the description of an object. To actually create an object, you must declare a variable based on the class. Such a variable is called an object. It is also referred to as an instance of the class. As done with regular variables, to create an object, start with a name for the variable preceded by $. You can use the following formula:
$variable-name = new class-name();
You must use the new keyword. Its job/role is to indicate that you want the variable to have or hold a reference to where the object is stored in the computer memory. The class keyword is followed by the name of the class and parentheses. Here is an example:
<?php
class House
{
}
$residence = new House();
?>
You can also declare a variable for the class in a function. Another formula to create an object is:
$object-name = " | 'class-name' | " $variable-name = new object-name();
Start with an initial variable name, named object-name in our formula. It must be preceded by $ and its name follows the rules of PHP variable names. Assign a quoted name of the class. The name of the class can be provided in single-quotes or double-quotes. Next, declare the actual variable you will use and initialize it with the first variable name as if it were a class. Here is an example:
<?php
class Sphere
{
}
$shape = "Sphere";
$ball = new $shape();
?>
Once you have done this, the second variable gives you access to the class.
Accessing a Member of a Class
Introduction
To access a member of a class outside the class, you use the -> operator between the name of the variable and the desired member. If the member is a variable, don't use the $ symbol. This would be done as follows:
<?php class House { $carpet = true; } $residence = new House(); $residence->carpet; ?>
If the class has more members, you can access them in the same way.
The Access Level of a Member of a Class
There are two ways a member of a class can be acccessed: By the other members of its class or by objects outside the class. When a class member can/must be accessed only by other members of the same class, the member is referred to as private. To create a private member, precede its name with the private keyword. Here is an example:
<?php
class House
{
private $beds = 12;
}
?>
Such a member cannot be accessed outside the class. A member that can be accessed outside the class is referred to as public. To create a public member in a class, precede the name of the member with the public keyword. Here is an example:
<?php class House { public $bedrooms = 3; public $bathrooms = 2.50; } $residence = new House(); ?>
Once you have accessed a member of a class, you can use it any way you want. If the member is holding a value, you can display it using echo. Here is an example:
<?php
class House
{
public $bedrooms = 3;
public $bathrooms = 2.50;
}
$residence = new House();
echo "Bedrooms: ";
echo $residence->bedrooms
?>
This would produce:
You can include it in a formatted string such as the double-quotes of echo. Here is an example:
<?php
class House
{
public $bedrooms = 3;
public $bathrooms = 2.50;
}
$residence = new House();
echo "Bedrooms: ";
echo $residence->bedrooms;
echo "<br>Bathrooms: $residence->bathrooms<br>";
?>
This would produce:
You can assign the member to a regular variable. You can involve it in an expression.
Introduction to the Properties of a Class
A class member that holds a value is called a property. As seen above for what we called members of a class, you primarily create a property by declaring a variable in the body of a class. AS as seen above, you must initialize the variable. Oncfe this has been done, you can access the property inside or outside the class. Here are examples of properties:
<?php class Employee { public $EmployeeNumber = 503808; public $FirstName = 'Naomy'; public $LastName = 'Kande'; public $HourlySalary = 22.25; } ?>
After doing this, you can then access the property outside the class. Here are examples:
<?php class Employee { public $EmployeeNumber = 503808; public $FirstName = 'Naomy'; public $LastName = 'Kande'; public $HourlySalary = 22.25; } $empl = new Employee(); echo "<p><b>Employee Record<b></p>"; echo "<table><tr> <td>Employee #:</td><td>$empl->EmployeeNumber</td></tr><tr> <td>First Name:</td><td>$empl->FirstName</td></tr><tr> <td>Last Name:</td><td>$empl->LastName</td></tr><tr> <td>Hourly Salary:</td><td>$empl->HourlySalary</td></tr> </table>"; ?>
This would produce:
Introduction to the Methods of a Class
A Function in a Class
A class can have functions in its body. A function created in a class is referred to as a member function or it is called a method. The role of a member function or method is to perform an action on behalf of the class. A method is primarily created like a regular function, only inside the class. Here is an example:
<?php
class Sphere
{
function show()
{
}
}
?>
In the body of the method, you can create any statement you want. As mentioned for the properties of a class, to access a method outside the class using an object, use the -> operator. Here is an example:
<?php
class Square
{
function show()
{
echo "Geometry - Square";
}
}
$paper = new Square();
$paper->show();
?>
$this Reference/Pointer
To let you access the members of a class from inside the class, PHP provides a special object named $this. This object can be used only by members (mostly methods) of a class and only inside the class. Based on this object, to access a member of a class inside the class, type $this followed by the -> operator and the desired member. Here is an example:
<?php class Sphere { public $radius = 26.50; function calculateArea() { $area = $this->radius * $this->radius * 4.00 * 3.14156; return $area; } function calculateVolume() { $area = $this->calculateArea(); return $area * $this->radius / 3.00; } function show() { echo "Geometry - Sphere<br>"; echo "Radius: $this->radius<br>"; echo "Area: " . $this->calculateArea() . "<br>"; echo "Volume: " . $this->calculateVolume() . "<br>"; } } $ball= new Sphere(); $ball->show(); ?>
This would produce:
The self Reference/Pointer
You can create an object of a class in its own body. To do this, declare the variable and initialize it using the self keyword in place of the name of the class. Here is an example:
<?php class Sphere { public $radius = 26.50; private function calculateArea() { $area = $this->radius * $this->radius * 4.00 * 3.14156; return $area; } private function calculateVolume() { $area = $this->calculateArea(); return $area * $this->radius / 3.00; } public function show() { $shape = new self; echo "Geometry - Sphere<br>"; echo "Radius: $shape->radius<br>"; echo "Area: " . $shape->calculateArea() . "<br>"; echo "Volume: " . $shape->calculateVolume() . "<br>"; } } $boule = "Sphere"; $ball = new $boule(); $ball->show(); ?>
The Access Level of a Method
Like a property, an access level can be applied to a method. The private and public rules are the same. By default, the access level of a method is public. This means that if you don't specify the access level of a method, the member function is automatically made public. If you want to hide a method from outside its class, make it private. Here are examples:
<?php class Sphere { public $radius = 26.50; private function calculateArea() { $area = $this->radius * $this->radius * 4.00 * 3.14156; return $area; } private function calculateVolume() { $area = $this->calculateArea(); return $area * $this->radius / 3.00; } function show() { echo "Geometry - Sphere<br>"; echo "Radius: $this->radius<br>"; echo "Area: " . $this->calculateArea() . "<br>"; echo "Volume: " . $this->calculateVolume() . "<br>"; } } $ball= new Sphere(); $ball->show(); ?>
If you want to indicate that a method can be accessed outside, either omit the access level or create it with the public keyword. Here is an example:
<?php
class Sphere
{
public $radius = 26.50;
private function calculateArea()
{
$area = $this->radius * $this->radius * 4.00 * 3.14156;
return $area;
}
private function calculateVolume()
{
$area = $this->calculateArea();
return $area * $this->radius / 3.00;
}
public function show()
{
echo "Geometry - Sphere<br>";
echo "Radius: $this->radius<br>";
echo "Area: " . $this->calculateArea() . "<br>";
echo "Volume: " . $this->calculateVolume() . "<br>";
}
}
$ball= new Sphere();
$ball->show();
?>
The Parameters of a Method
As seen for functions, a method can take one or more parameters. You include the parameter(s) in the parentheses of the method. In the body of the method, do whatever you want. Outside the class, you can create an object and access the method from it. If the method is taking an argument, pass the appropriate value(s) to it.
A Constant in a Class
If you need to use an unchanged value among the members of a class, you can create a constant for it. To do this, use the const keyword preceding the name of the constant and assign the desired value to it. Wherever you need to use the constant throughout the class, precede its name with the self keywords. The keyword and the constant must be separated by the :: operator. Here is an example:
<?php class Sphere { public $radius = 32.35; const PI = 3.14156; private function calculateArea() { $area = $this->radius * $this->radius * 4.00 * self::PI ; return $area; } private function calculateVolume() { $area = $this->calculateArea(); return $area * $this->radius / 3.00; } public function show() { $shape = new self; echo "Geometry - Sphere<br>"; echo "Radius: $this->radius<br>"; echo "Area: " . $shape->calculateArea() . "<br>"; echo "Volume: " . $shape->calculateVolume() . "<br>"; } } $boule = "Sphere"; $ball = new $boule(); $ball->show(); ?>
Class Construction
Introduction
A constructor of a class is a special method that lays the primary foundation of a class. To have a constructor, create a method named __construct. Here is an example:
<?php
class Employee
{
function __construct()
{
}
}
?>
If you add a constructor to a class, when an object of that class is created, the constructor immediately executes before any other action is performed on the object. You can use this characteristic to take any early action you want. For example, you can initialize the members of the class in the constructor. Here is an example:
<?php
class Employee
{
public $EmployeeNumber = 0;
public $FirstName = '';
public $LastName = '';
public $HourlySalary = 0.00;
function __construct()
{
$this->EmployeeNumber = 503808;
$this->FirstName = 'Naomy';
$this->LastName = 'Kande';
$this->HourlySalary = 22.25;
}
}
$empl = new Employee();
echo "<p><b>Employee Record<b></p>";
echo "<table><tr>
<td>Employee #:</td><td>$empl->EmployeeNumber</td></tr><tr>
<td>First Name:</td><td>$empl->FirstName</td></tr><tr>
<td>Last Name:</td><td>$empl->LastName</td></tr><tr>
<td>Hourly Salary:</td><td>$empl->HourlySalary</td></tr>
</table>";
?>
A Constructor with Parameters
A contructor of a class is primarily a method. As such, it can take a parameter. Here is an example:
<?php
class Cube
{
function __construct($s)
{
}
}
?>
After doing this, when creating an object, this constructor allows you to pass a value to the class. To do this, when creating object, provide a value in the parentheses of the class. Here is an example:
<?php class Cube { function __construct($s) { } } $dice = new Cube(42.58); ?>
As you might have realized by now, the primary role of a property is to hold a value. This is usually done by assigning a value to the property. Here is an example:
<?php
class Cube
{
public $Side = 0.00;
function __construct($s)
{
}
}
$dice = new Cube(42.58);
?>
In many languages and in traditional object-oriented programming, a property has two roles: 1) it can take a value from outside the class to make such a value available to the internal members of the class; 2) It holds a value from the class and makes that value available to the outside objects. One of the ways a property accomplishes this is to receive a value that is passed to the constructor. To make this happen, you can assign the argument to the property inside the constructor. This can be done as follows:
<?php
class Cube
{
public $Side = 0.00;
function __construct($s)
{
$this->Side = $s;
}
}
$dice = new Cube(42.58);
?>
When this has been done, the property now holds the value passed to the constructor. That value can be accessed and used from both the inside and the outside of the class. Here are examples:
<?php class Cube { public $Edge = 0.00; function __construct($e) { $this->Edge = $e; } function SideArea() { return $this->Edge * $this->Edge; } function Volume() { return $this->Edge * $this->Edge * $this->Edge; } } $dice = new Cube(42.58); echo "Edge: $dice->Edge<br>"; echo "Side Area: "; echo $dice->SideArea() . "<br>"; echo "Volume: "; echo $dice->Volume() ?>
A Destructor
A destructor is a special method that is used to clean the computer memory that was used by an object when that object is not used anymore. To get a destructor, create a method named __destructor. Here is an example:
<?php
class Cube
{
public $Edge = 0.00;
function __construct($e)
{
$this->Edge = $e;
}
function SideArea()
{
return $this->Edge * $this->Edge;
}
function Volume()
{
return $this->Edge * $this->Edge * $this->Edge;
}
function __destructor()
{
}
}
$dice = new Cube(42.58);
echo "Edge: $dice->Edge<br>";
echo "Side Area: ";
echo $dice->SideArea() . "<br>";
echo "Volume: ";
echo $dice->Volume()
?>
Unlike some languages (like C++ and some implementations of Pascal), this method is not required.
Using an Object to Produce Many Values
When you use a function that returns a value, the function returns a single regular value. One of the advantages of a class as opposed to a primitive type is that it can hold many values. Based on this, if a function returns an object, it makes it possible for the function to return many values.