Hướng dẫn classes in php tutorialspoint

Introduction

Class is a user defined data type in PHP. In order to define a new class, PHP provides a keyword class, which is followed by a name. Any label that is valid as per PHP's naming convention [excluding PHP's reserved words] can be used as name of class. Constituents of class are defined in curly bracket that follows name of class

Syntax

class myclass{
   //
}

Class may contain constants, variables or properties and methods - which are similar to functions

Example of class

This example shows how a Class is defined

Example

Function defined inside class is called method. Calling object's context is available inside a method with a pseudo-variable $this. If method is defined as static, it is accessed with name of class. Calling a non-static method statically has been deprecated in PHP 7

The new operator declares a new object of given class. ame of class followed by paentheses should be mentioned in front of new keyword. An uninitialized object [or with default values to properties] is created if there are no arguments inside parentheses. If class provides definition of constructor with parameters, matching number of arguments must be given. Class must be defined before creating instance [or object]

Example

 Live Demo

Output

This will produce following result. −

Hello

Updated on 18-Sep-2020 09:47:28

  • Related Questions & Answers
  • PHP Variable Basics
  • PHP Inherit the properties of non-class object?
  • Object and class in Java
  • Basics of React.js Routing
  • Microcomputer Basics
  • Microprocessor Basics
  • Microcontroller Basics
  • HTML Basics
  • Basics of C++ Programming Language?
  • Basics of Three-Phase Electricity
  • Check whether property exists in object or class with PHP
  • C# Object Creation of Inherited Class
  • Java Object Creation of Inherited Class
  • Method of Object class in Java
  • Difference between Object and Class in Java

PHP 7 is the most awaited and is a major feature release of PHP programming language. PHP 7 was released on 3rd Dec 2015. This tutorial will teach you the new features of PHP 7 and their usage in a simple and intuitive way.

Audience

This tutorial has been prepared for PHP developers from a beginner’s point of view. After completing this tutorial, you will find yourself at a moderate level of expertise in the knowledge of PHP from where you can take yourself to next levels.

Prerequisites

We assume that you already know about the older versions of PHP and now you can start learning the new features of PHP 7.

Introspection is a common feature in any programming language which allows object classes to be manipulated by the programmer. You’ll find introspection particularly useful when you don’t know which class or method you need to execute at design time.

Introspection in PHP offers the useful ability to examine classes, interfaces, properties, and methods. PHP offers a large number functions that you can use to accomplish the task. In order to help you understand introspection, I’ll provide a brief overview of some of PHP’s classes, methods, and functions using examples in PHP to highlight how they are used.

During this article, you’ll see a couple examples of how to use some of the most useful PHP’s introspection function and a section dedicated to an API that offers functionality similar to introspection, the Reflection API.

PHP Introspection Functions

In the first example, I’ll demonstrate a handful of PHP’s introspection functions. You can use these functions to extract basic information about classes such as their name, the name of their parent class, and so on.

  • class_exists[] – checks whether a class has been defined
  • get_class[] – returns the class name of an object
  • get_parent_class[] – returns the class name of an object’s parent class
  • is_subclass_of[] – checks whether an object has a given parent class

Here is the example PHP code that contains the definition for Introspection and Child classes and outputs information extracted by the functions listed above:

Chủ Đề