Information



Tags



Share

Object-oriented programming (OOP) in PHP for Beginners

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement



Definition

I know that many web developers do not like using PHP in their work because of its incoherent syntax and parameters. Conversely, I am a big fan of PHP and like to use it on my website and other works. So that I decided to write a tutorial series about PHP to explain its features which I used mostly; you can find former tutorials on Articles. And, it would be incongruous to not continue with PHP classes and objects.

Class(1)

Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.

The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$.

A class may contain its own constants, variables (called "properties"), and functions (called "methods").

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object). As of PHP 7.0.0 calling a non-static method statically from an incompatible context results in $this being undefined inside the method. Calling a non-static method statically from an incompatible context has been deprecated as of PHP 5.6.0. As of PHP 7.0.0 calling a non-static method statically has been generally deprecated (even if called from a compatible context). Before PHP 5.6.0 such calls already triggered a strict notice.

Object(2)

To create a new object, use the new statement to instantiate a class.

Code

Use public to define public variables.

Use the Scope Resolution Operator (also called Paamayim Nekudotayim) to reach static, constant, and overridden properties or methods of a class, well-explained on the code.

Also, you can either write a new function to save each new user to the database or add new features on the createUser function by using MySQLi to improve my tutorial code.


<?php
class user {
  // Define a constant.
  const USER_TYPE = "Premium";
  // Define a private static variable only accessible in the class itself.
  private static $PATH = "Pictures/";
  // Define public variable which can be changed out of the class itself.
  public $firstName;
  public $lastName;
  public $id;
  public $img;
  // Create a function for each new user.
  function createUser($v_1, $v_2, $v_3, $v_4){
   $this->firstName = $v_1;
   $this->lastName = $v_2;
   $this->id = $v_3;
   $this->img = $v_4;
  }
  // Show the former defined user on the database.
  function showUser(){
   echo '<div class="USER">';
   echo '<img src="'.self::$PATH.$this->img.'" />';
   echo '<p>'.$this->firstName.' '.$this->lastName.'<br> { '.$this->id.' }<br><br>';
   echo '<span style="color:red;">'.self::USER_TYPE.'</span></p>';
   echo '</div>';
  }
}

// Create an object named person to get functions and variables from the user class.
$person = new user();
// Create and show a new user.
$person->createUser("Jane", "Doe", "Avengers123", "userDefault.png");
$person->showUser();
?>


Result:

Jane Doe
{ Avengers123 }

Premium



References

(1) http://php.net/manual/en/language.oop5.basic.php

(2) http://php.net/manual/en/language.types.object.php