© Gunnard Engebreth, Satej Kumar Sahu 2023
G. Engebreth, S. K. SahuPHP 8 Basicshttps://doi.org/10.1007/978-1-4842-8082-9_8

8. Objects

Gunnard Engebreth1   and Satej Kumar Sahu2
(1)
Madison, WI, USA
(2)
Bangalore, India
 

So far, we have covered several data types, including the string, integer, and float. You’ve learned how to use strings, integers, and arrays. Each of these types has their own benefits and limitations. An integer cannot use the letter “s” as a value, and a string can contain an integer of “1.” With arrays, you learned about the idea of a compound data type. This data type allows for combining and intermixing of different elements. An array can contain both letters and numbers, contain specific key-pair values (associative array), or just contain an organized set of data. The end result is that values of more than one type can be stored together in a single variable.

In this brief chapter, we will focus on a PHP data type we touched on in Chapters 2 and 4: the object.

Please notice that, in general, classes and objects are the two main aspects of object-oriented programming and are therefore very important.

To understand how classes and objects are interlinked, we could say that a class is a template for an object and an object is an instance of a certain class.

Similar to arrays, you can set and use multiple types of information, from strings to all types of numbers. Objects, however, give you the ability to define specific functionality. This functionality is set in the class definition.

So, when you create an individual object, it will inherit all the properties and behaviors from the class it’s linked to, but each object will still have different values for the properties.

Objects are user-defined classes that can store both values and functions and must be explicitly declared.

Let’s take a look at some basic examples.
<?php
class Vegetable {
  // Properties
  public $name;
  public $color;
  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

Here you are declaring a class named Vegetable. This class contains both properties and methods. Remember that properties are variables and methods are functions. The two properties are $name and $color. The two methods are set_name and get_name. They are commonly referred to as “getters and setters.” These types of methods are common with objects because you are constantly “getting” and “setting” values to the class properties. It is very convenient to create these helper type functions. If you have these functions in your objects, you will only need to remember to $vegetable->get_name(); and $vegetable->set_name();.

Here is another example of an object:
<?php
class SayHi{
   function hi(){
      echo "Hello World";
   }
}
$obj=new SayHi;
$obj->hi();
?>

Output: Hello World

In some cases, you may want to create an object on the fly. PHP has stdClass, which allows you to do this.
<?php
$obj=new stdClass;
$obj->name="gunnard";
$obj->age=26;
$obj->twitter="@gunnard";
print_r($obj);
?>

Output

This will produce following result:
stdClass Object(
   [name] => gunnard
   [age] => 26
   [twitter] => @gunnard
)
Let’s start fresh with a basic class and see how changes in the class affect the object. You will create a Beverage class to classify and track information on beverages at the pizzeria that you run.
<?php
class Beverage {
     public $name;
     public $type;
     public $temperature;
     public $price;
     public $sale;
}
In order to use this class as an object, you need to instantiate it. This is done through the new keyword.
<?php
$cola = new Beverage();
?>
Now you have an object with the name $cola, which contains the properties you defined in the class Beverage. You can use this object by assigning values to the properties with the -> operator. This will allow you to assign specific values to each property.
<?php
$cola = new Beverage();
$cola->name = "Rocky Cola";
$cola->type = "Soda";
$cola->teperature = "45 f";
$cola->price = "0.50";
$cola->sale = null;
?>
Now that you can set values to your class properties, let’s add class methods or functions within a class that allow objects to manipulate data. For example,
<?php
class Beverage {
     public $name;
     public $type;
     public $temperature;
     public $price;
     public $sale;
}
function getMenuName() {
     return $this->type:.' '.$this->name.' '.$this->price;
}
?>

With getMenuName, the intention is to display the type, name, and price of the beverage. This can be used when displaying the full menu of the restaurant. Instead of using the object to return the name, type, and price, and THEN formatting it, you can take care of all of that in this method.

The $this variable refers to the current object in use. When you invoke the getMenuName() method, $this refers to the specific object that calls the method. Object methods are accessed similarly to properties, using the object operator ->, but as with any function, there are parentheses at the end, as in ().

Summary

In this chapter, you learned how to use a PHP object, which is another compound data type. It is similar to an array, which can be set and use with multiple types of information, from strings to all types of numbers.

In the next chapter, you will learn how …

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset