Understanding Basic OOP Concepts in PHP: Part 2

Introduction

Now that you know from my previous article how to create a class, however if you have not then please see my previous article first:

Understanding Basic OOP Concepts in PHP: Part 1


After the completion of that article, this article tells you about properties. Basically class properties are very similar to variables. For example object properties are responsible for storing a single value, or an array of values or it may be another object.

Understand property visibility

Before proceeding with how to create properties in PHP, let's first go with another important concepts of classes known as visibility. Formally speaking, if we talk about the level of visibility or say the visibility level of properties then you can divide that into public, private and protected levels.

Public properties

As the name implies, if you are declaring a property as Public, then you can access it from anywhere in your PHP script, inside or outside of the class, you can also read and change it from anywhere in your PHP script.

Private properties

It can be accessible within the same class (in other words in which it is declared). So if you create a property as private, then it is only used by those methods, that are declared inside the class and if you try to access a private property outside of the class then PHP will generate a fatal error.


Protected
properties

It is the same as private properties but a little different; it is accessible outside of the class with the condition that only classes that inherit the class.

If we say it n simple terms then it is a good idea to avoid creating public properties whenever possible. Instead create private properties, then to create those methods, that allow code outside of the class to access those properties. Then you will know how to exactly handle how your class's properties are accessed.

How to declare properties

To declare properties, provide the visibility level of public, private or protected depending on your need followed by the propert's name (precedes by the $ symbol) as in the following:

class Example
{

public $propertyName1;
private $propertyName2;
protected $propertyName3;
}

Note that you can also initialize the properties at the time of declaration.

class Example
{
public $propertyName1=123;
}

In this case, whenever an object of the Example class is created, the object's public $propertyName1 properties default to the value 123.

Access properties

After creating a class's properties, you can access corresponding objects property values from within your calling code by using the following syntax:

$object->property;

The given following example shown you, how define properties then set and read their values.

Example

In this example I setup three public properties and assign a default value to each of them, and for accessing the properties variables on an object by object basis using the characters "->" in conjuction with an object variable and property name.

<?
php
class
Products
{
//
Class Properties
 
   
public $proTitle = "SoldOut";
   
public $proName = "Inprogress";
   
public $proPrice = "$200";
 
}
 
//
Class Object 1
 
$
product1 = new Products();
 
//
Object 1 Properties
 

echo
"<h2>Object 1</h2>";
echo
"<b>Product title:</b> ".$product1->proTitle."</br>";
echo "
<b>Product Name:</b> ".$product1->proName."</br>";
echo "
<b>Product Price:</b> ".$product1->proPrice."</br>";
 
//Class Object 2
 
$product2 = new Products();
 
//Assigning New Values
 
$product2->proTitle = "Product title";
$product2->proName = "Product Name";
$product2->proPrice = "$300";
 
//Object 2 Properties
 
echo "
<h2>Object 2</h2>";
echo "
<b>Product title:</b> ".$product2->proTitle."</br>";
echo "
<b>Product Name:</b> ".$product2->proName."</br>";
echo "
<b>Product Price:</b> ".$product2->proPrice."</br>";
 
//Class Object 3
 
$product3 = new Products();
 
//Assigning New Values
 
$product3->proTitle = "Jeans";
$product3->proName = "Trigger";
$product3->proPrice = "$400";
 
//Object 3 Properties
 
echo "
<h2>Object 3</h2>";
echo "
<b>Product title:</b> ".$product3->proTitle."</br>";
echo "
<b>Product Name:</b> ".$product3->proName."</br>";
echo "
<b>Product Price:</b> ".$product3->proPrice;
?>


Output


properties-in-php.gif

Static Properties

You are experienced with static function variables in earlier articles. You just add the "static" keyword before the property name.

class Example
{
public static $myproperty;
}

A static member of a class independent of any particular object derived from that class. Use two colons(: :), after the class name for accessing the static property. For example:

Example : : $myproperty=9087;

Example

Within the Example class, a static property, $value , is declared and also initialized to 8976. Then, outside the class definition, the static property value, 8976, is displayed.

<?php

class Example

{

static public $value = 8976;

}

echo Example::$value;// Displays "124"

?>

Output

static-properties-in-php.gif


Similar Articles