Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
objects
communitycreator

How to create an object in PHP

NDUKWE CHIDERA K.

Overview

Object Oriented Programming is a programming concept where real-world items are given abstract groupings called a class. Subgroups known as subclasses can be made from the larger groups as well.

Classes have basic descriptive variables and methods for each item, what does, and how it is created. These distinct items are known as objects, which may have characteristics derived from the class as well as others that are unique to it.

For example, consider a class called “footwear”. This class has subclasses, and it is made up of members which are similar in many ways. Some subclasses of footwear may be:

  • Shoes
  • Sandals
  • Boots
  • Flip flops

These subclasses can have specific items with the same characteristics and properties. Some of these objects in the shoe subclass may be loafers, sneakers, or oxford shoes.

Sample object creation

Creating an object in PHP

The basic description given in the diagram above is used every day by developers to write object-oriented code, where there is a logical thought process that consists of:

  1. Identifying program components that will eventually perform similar functions.
  2. Creating these program components with their properties/attributes and functions/methods.
  3. Identifying unique use cases for each component and applying them to solve such issues.

Consider mysqli classes in PHP. These have functions like query, connect, and num_rows, as well as properties like database names, host names, passwords, and user information.

Objects which are unique use cases of these classes can be new database connections from the mysqli class. These new objects can access all methods and properties of this class.

Creating a new mysqli object

To create a new object in PHP, you must use the new keyword.

Syntax

$objectName = new className();

Code

In the code below, a new mysqli object was created. This new object now has access to the class methods using the arrow -> symbol.

<?php

$dbconn = new mysqli("localhost", "root", "","mydb");
//i can access methods in mysqli class like below
if($dbconn -> connect_error){
  // do something
}
?>

In the next code block, I created my own custom class with some properties and methods. Then I used the new command to create some instances of class objects. With the help of the arrow -> symbol, these objects can make use of the methods available in the house class.

<?php 
class house {
  // Properties
  public $name;
  public $size;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}

$condo = new house();
$duplex = new house();
$condo->set_name('personal condo');
$duplex->set_name('detached duplex');

echo $condo->get_name();
echo "\n";
echo $duplex->get_name();
?>

RELATED TAGS

php
objects
communitycreator
RELATED COURSES

View all Courses

Keep Exploring