Home >Backend Development >PHP Tutorial >(Advanced) PHP Object-Oriented - Classes and Objects

(Advanced) PHP Object-Oriented - Classes and Objects

黄舟
黄舟Original
2017-02-06 10:16:141012browse

The following is the text of the article:

Basic concepts

Object-oriented programming (Object Oriented Programming, OOP, object-oriented programming) is a computer programming architecture. A basic principle of OOP is that a computer program is composed of a single unit or object that functions as a subroutine. OOP achieves three goals of software engineering: reusability, flexibility, and extensibility.

PHP has improved its support for OOP after version 4.0. For small applications, it may be simpler and more efficient to use traditional procedural programming. However, for large and complex applications, OOP is a choice that must be considered.

Class

A class is a collection of objects with the same properties and services. It provides a unified abstract description for all objects belonging to this class, which includes two main parts: properties and services. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attribute description and service description.

Object

The object is in the system An entity used to describe objective things. It is a basic unit that constitutes a system. An object consists of a set of properties and a set of services that operate on the set of properties.

The relationship between classes and objects is like the relationship between molds and castings. The instantiation result of a class is an object, and the abstraction of a type of object is a class.

Object-oriented programming covers a wide range of topics. This tutorial only introduces basic concepts and applications in PHP.

Class

Use the keyword class to declare a class, followed by the name of the class, and the body is enclosed in {} symbols.

Syntax:

class class_name{ ...... }

The class contains attributes and methods.

Attributes

In the body of a class, you can declare special variables called attributes. In PHP V4, properties must be called with the keyword var. This is still legal syntax, but is mainly for backward compatibility. In PHP V5, properties must be declared public, private or protected. Can be found in Keywords: Can we have a little privacy here? Read about these qualifiers in . But now declare all properties as public in the example. Listing 1 shows a class that declares two properties.

Class that declares two attributes

class Dictionary {
   public $translations = array();
   public $type ="En";
}

For example, if you define a class of people, then the person's name, age, gender, etc. can be regarded as attributes of the class of people.

Method

By declaring a function in the class definition, a method of the class is created.

grammar:

class class_name{ function function_name(arg1,arg2,……) { 函数功能代码 } }

类的应用

一个定义了属性和方法的类就是一个完整的类了,可以在一个类里面包含一个完整的处理逻辑。使用 new 关键字来实例化一个对象以便应用类里面的逻辑。可以同时实例化多个对象。

语法:

object = new class_name();

实例化一个对象后,使用 -> 操作符来访问对象的成员属性和方法。

语法:

object->var_name; object->function_name;

如果要在定义的类里面访问成员的属性或者方法,可以使用伪变量 $this 。$this 用于表示 当前对象 或 对象本身 。

例子:

<?php 
class Person { 
//人的成员属性 
var $name; //人的名字 
var $age; //人的年龄 
//人的成员 say() 方法 
function say() { 
echo "我的名字叫:".$this->name."<br />"; 
echo "我的年龄是:".$this->age; 
} 
} //类定义结束 

//实例化一个对象 
$p1 = new Person(); //给 $p1 对象属性赋值 
$p1->name = "张三"; 
$p1->age = 20; 
//调用对象中的 say()方法 
$p1->say(); 
?>

运行该例子,输出:

我的名字叫:张三 我的年龄是:20

上面的例子演示了一个简单的基于面向对象的 PHP 应用。

以上就是(进阶篇)PHP面向对象- 类与对象的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn