PHP Class Public Properties: Mastering OOP

This is the sequential post of mastering oop.  I will recommend to read short but comprehensive tutorial about Basics of PHP Class before proceeding this tutorial, if you are absolutely new to object oriented programming. This tutorial will share a knowledge about,

  • Concept of Class Properties
  • Keyword: public

What are Class Properties

Classes can define special variables called properties. A property, also known as a member variable, holds data that can vary from object to object.

A property in a class looks similar to a standard variable except that you must precede your declaration and assignment with a visibility keyword. This can be public, protected, or private, and it determines the scope from which the property can be accessed.

I will write about “protected” and “private” visibility scope in the later tutorials.

Let’s declare some properties using the public keyword,

/*
|------------------
| Class Tutorial
|------------------
*/

class Product {

	/*
	|------------------
	| Class Properties
	|------------------
	*/

	public $productTitle = "Product Title";
	public $productName = "Product Name";
	public $productPrice = 0;

}

Points to remember are,

  • I set up three properties, assigning a default value to each of them.
  • Any objects that I instantiate from the Product class will now be prepopulated with default data.
  • The public keyword in each property declaration ensures that I can access the property from outside of the object context.

Access Class Properties

You can access property variables on an object-by-object basis using the characters ‘->’ in conjunction with an object variable and property name, like this:

/*
|------------------
| Class Object 1
|------------------
*/

$product1 = new Product();

/*
|------------------
| Object 1 Properties
|------------------
*/

echo "<h2>Object 1</h2>";
echo $product1->productTitle;
echo "<br />";
echo $product1->productName;
echo "<br />";
echo $product1->productPrice;
echo "<br />";

/*
|------------------
| Output
|------------------
*/

Object 1
Product Title
Product Name
0

Keyword: public

The key concept of public property is,

  • You can assign values to them just as you can read them, replacing any default value set in the class

Let’s create two more objects to understand the concept of class and oop.

/*
|------------------
| Class Object 2
|------------------
*/

$product2 = new Product();

/*
|------------------
| Assigning New Values
|------------------
*/

$product2->productTitle = "Apple iPhone 4G";
$product2->productName = "iPhone 4G";
$product2->productPrice = "$399";

/*
|------------------
| Object 2 Properties
|------------------
*/

echo "<h2>Object 2</h2>";
echo $product2->productTitle;
echo "<br />";
echo $product2->productName;
echo "<br />";
echo $product2->productPrice;
echo "<br />";

/*
|------------------
| Class Object 3
|------------------
*/

$product3 = new Product();

/*
|------------------
| Assigning New Values
|------------------
*/

$product3->productTitle = "Apple iPad";
$product3->productName = "iPad";
$product3->productPrice = "$700";

/*
|------------------
| Object 3 Properties
|------------------
*/

echo "<h2>Object 3</h2>";
echo $product3->productTitle;
echo "<br />";
echo $product3->productName;
echo "<br />";
echo $product3->productPrice;
echo "<br />";

/*
|------------------
| Output
|------------------
*/

Object 2
Apple iPhone 4G
iPhone 4G
$399

Object 3
Apple iPad
iPad
$700

It is very easy to understand by our above example,

  • Object can assign or replace “class public properties” directly.
  • A class is a template for generating objects, and each object has its own properties.
  • An object is data that has been structured according to the template defined in a class.

Full Tutorial

/*
|------------------
| Class Tutorial
|------------------
*/

class Product {

	/*
	|------------------
	| Class Properties
	|------------------
	*/

	public $productTitle = "Product Title";
	public $productName = "Product Name";
	public $productPrice = 0;

}

/*
|------------------
| Class Object 1
|------------------
*/

$product1 = new Product();

/*
|------------------
| Object 1 Properties
|------------------
*/

echo "<h2>Object 1</h2>";
echo $product1->productTitle;
echo "<br />";
echo $product1->productName;
echo "<br />";
echo $product1->productPrice;
echo "<br />";

/*
|------------------
| Class Object 2
|------------------
*/

$product2 = new Product();

/*
|------------------
| Assigning New Values
|------------------
*/

$product2->productTitle = "Apple iPhone 4G";
$product2->productName = "iPhone 4G";
$product2->productPrice = "$399";

/*
|------------------
| Object 2 Properties
|------------------
*/

echo "<h2>Object 2</h2>";
echo $product2->productTitle;
echo "<br />";
echo $product2->productName;
echo "<br />";
echo $product2->productPrice;
echo "<br />";

/*
|------------------
| Class Object 3
|------------------
*/

$product3 = new Product();

/*
|------------------
| Assigning New Values
|------------------
*/

$product3->productTitle = "Apple iPad";
$product3->productName = "iPad";
$product3->productPrice = "$700";

/*
|------------------
| Object 3 Properties
|------------------
*/

echo "<h2>Object 3</h2>";
echo $product3->productTitle;
echo "<br />";
echo $product3->productName;
echo "<br />";
echo $product3->productPrice;
echo "<br />";

You may be interested to know about,