How To Use Classes And Objects In PHP

How To Use Classes And Objects In PHP

·

4 min read

In this post, we will be learning how exactly to use PHP to create classes, properties, and operations.

Class You can think of a class as a programmer-defined data type. And this data type will have its own custom properties and method that you define for it.

To create a class, you need to use the keyword class.

At minimum to create a class you need to write some code that looks like this.

Syntax

class classname
{
}

But as you guessed, the class above is absolutely useless. In order for a class to be useful, the class needs to have properties and operations.

Class Properties

Properties are variables the class has. Every object created from this class must have these properties. To create properties, you write within the class something like the following.

class classname
{
public $attribute1;
public $attribute2;
}

The first word(public) determines the visibility of the attribute; you can adjust the visibility of a particular property using the keywords public private or protected.

The second word($attribute1) is the name of your particular property. You can name it whatever you wish. However, just like any variable it is best to use a name that describes what it represents.

You can think of properties essentially as "variables that each object belonging to this class must have". Each individual object may have a different value for this property, but they must all have the property.

In truth, even the above class is quite useless because it does not have a constructor function or any methods.

Class Methods

You can think of methods as "functions that each object belonging to this class must have".

To create an operation, you basically declare a function in the class definition.

The code below shows a function with two operations that do absolutely nothing. Syntax

class classname
{
public a(){};
public b($param1, $param2){};
}

The method a() takes no parameters and b() takes two parameters.

Every instance of classname will have a method of a() and b() and they will execute the exact same code.

Of course, however, they may or may not yield the same results because each instance may have different property values which may affect the results of the methods.

Class Properties And Method Syntax: More Details

As you already know from above, a class needs to have properties and methods to be useful. These are the traits and actions that all objects of the particular class share.

Here's some code that shows how properties and methods can be declared together.

class classname
{
public property_one;
public property_two;
function operation_one($param)
{
echo $this->property_one;
}
}

So as you can see above, you declare what properties a particular class is supposed to have just after you declared your class name(right below the curly brackets).

Depending on what type you set it, it can be used in different contexts, the above we declare all properties as public properties.

So when we want to use a property in a particular class, we simply write $this->property.

This gives access to the specific property that belongs to that object.

To work with the property in your class add $this-> and omit the $ you added during the property's declaration.

So that is how you access a property within your class; if you want to access the property outside of your class, you simply write $object->property.

Example

class classname
{
}
$a = new classname();
$a->attribute = "value";
echo $a->attribute
;

What You Want To Do In Real life

The above code where you access the property directly isn't actually a good idea.

In reality, you should be accessing properties through accessor functions.

This is the purpose of a principle called encapsulation; instead of directly accessing (changing or using) properties, we use to set and get functions.

Here is a better example of what to do in real life.

Example

class classname
{
}
$a = new classname();
$a->setAttribute = "value";
echo $a->getAttribute()
;

Private

Private means that this property or method can only be accessed within this particular class.

What might this mean exactly?

For example, let's say the previous example we had set the properties and methods to "private", would the following populations still have value?

The answer is "no", since the methods and properties are private, we can only access them within the class.

However, what can we do if we wish to access the particular property?

We can access them using a public function.

Protected

This third type called protected is the most confusing of the three types to understand.

Protected properties and methods can be accessed within the class as well as within the subclasses of the class.

For now, you can think of them as halfway between public and private.

Final Note

While it is a legitimate code to leave out the word "public", writing it is generally considered good practice.