(Advanced article) PHP Object-oriented-classes and objects

Source: Internet
Author: User
Tags php class
Here is the text of the article:

Basic concepts

OO programming (Object Oriented programming, OOP, object-oriented programming) is a computer programming architecture. One of the basic tenets of OOP is that a computer program is composed of a single unit or object that can act as a subroutine. OOP achieves the three goals of software engineering: reusability, flexibility, and extensibility.

PHP has perfected the support for OOP after the 4.0 release. For small applications, using traditional procedural programming can be simpler and more efficient. However, for large, complex applications, OOP is an option that has to be considered.

Class

A class is a collection of a set of objects that have the same properties and services. It provides a unified, abstract description of all objects that belong to the class, including two main parts of properties and services. In an object-oriented programming language, a class is a separate program unit that should have a class name and include a description of the property and two main parts of the service description

Object

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

The relationship between a class and an object is like the relationship between a mold and a casting, and the instantiation of a class is the object, and the abstraction of a class object is the classes.

With respect to object-oriented programming, this tutorial covers only basic concepts and applications in PHP.

Class

Use the keyword class to declare a class, followed by the name of the class, surrounded by the {} symbol.

Grammar:

Class class_name{...}

The class contains properties and methods.

Property

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

Class that declares two properties

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

For example, if you define a person's class, then the person's name, age, gender, and so on can see the nature of this class.

Method

By declaring a function in the class definition, you create a method of the class.

Grammar:

Class class_name{function function_name (arg1,arg2,......) {function function code}}

Application of the class

A class that defines properties and methods is a complete class that can contain a complete processing logic within a class. Use the New keyword to instantiate an object to apply the logic inside the class. You can instantiate multiple objects at the same time.

Grammar:

Object = new Class_name ();

After instantiating an object, use the-operator to access the object's member properties and methods.

Grammar:

object->var_name; object->function_name;

If you want to access the properties or methods of a member within a defined class, you can use a pseudo-variable $this. $this used to represent the current object or the object itself.

Example:

<?php class Person {//Member attribute var $name;//person's name Var $age;//person's age//person's member say () method function say () {echo "My name is:". $this-& Gt;name. " <br/> "; echo "My Age is:". $this->age; }}//class definition End//instantiation of an object $p 1 = new person (); Assigning values to $p 1 Object properties $p 1->name = "Zhang San"; $p 1->age = 20; The Say () method in the calling Object $p 1->say ();?>

To run the example, output:

My name is: Zhang San my age is: 20

The example above demonstrates a simple object-oriented PHP application.

Above is (Advanced article) PHP Object-oriented-class and object content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.