Next level PHP with Composite Data Types

PHP Composite Data Types. Aray

In my last blog post on PHP data tapes, I discussed the four scalar PHP data types. Today, we will continue with composite data types.

To recap, the scalar data types are good for variables representing a singular item, like price. But most of the time we are interested in a price of more than one item.

That is where composite data types come in.

Composite data types will take our game to the next level. They will help us do something meaningful with the data we have. For example, compare a price point of an item from different stores.

The first composite data type we will discuss is an Array and the second one is Object.

The first composite data type: Array

An array is like a big box, of boxes. Each of the small boxes has a unique id. This unique id is either automatically assigned to each element or explicitly given by us.

PHP Composite Data Types.

In PHP, we can declare an array using the array() function or square brackets. For instance, the following snippet shows creating an array of two elements.

$names = ["Abebe", "Abel"];
$names2 = array("Ababe", "Abel");

In both cases, PHP creates an indexed array for us. Hence, the first item in the array which is “Abebe” is positioned at an index of 0.

PHP Composite Data Types. Aray
Out array after inserting elements

We can also use the same syntax to create an associative array. Which is similar to an indexed array, but we give unique identifiers explicitly. Here is an example:-

$name = ["n1"=>"Abebe", "n2"=>"Abel"];
$name = array("n1"=>"Abebe", "n2"=>"Abel");

As you can see, the syntax is very similar to indexed arrays. The only difference being the arrow sign. At the left of the arrow is the id and the right side holds the value.

Associative arrays are very common in composite data types in PHP. As a result, I strongly suggest you taking few minutes and practicing them.

The second composite data type: Object

A person doesn’t just have a name. In fact, a person is more than a number with a number of attributes and behaviors. As a result, representing a person in an array will get out of hand very quickly. That is where objects come in.

Objects are a great addition as composite data types. They really do take your programming skills to the next level but might complicate it too. The reason being, OOP is very wide and it requires its own mindset.

However, we can take a quick look at objects and how they are used in PHP. As objects are composite data types, they are like an array. However, objects come from a blueprint. And, we call this blueprint a Class.

A class is a type that specifies the property and behavior of an object. Hence, like we said a person is not just a name. A person has height, width, weight, address, etc.

Also, what makes a person really a person is definitely what he/she does. A person eats, sleep, works, speaks, writes, etc.

We can bring all this conceptual abstraction of a person to live in our programs using Classes. Here is a snippet to illustrate.

class Person {
  private $name;
  private $address;

  public function write() {
    echo "writing";
  };
}

The person class from above has few attributes of a Person, which we call properties, and behavior, represented by that function, which we call method.

We can create an instance of that class Person, by using the new keyword.

$abebe = new Person();

We can do a whole lot with Objects and OOP. This is just a quick example, I implore you to take a dive into OOP principles with PHP.

Recapping Composite Data Types

This is of course a quick introduction and recap of composite data types. However, it is wise to relook and relearn this section of PHP in detail. I believe this is one of the areas that hold myself and my students back.

Your subscription could not be saved. Please try again.
Your subscription has been successful.

Learn Modern JS, CSS, & PHP

Subscribe to learn modern PHP in the most simple way.