PHP Classes and objects

Source: Internet
Author: User

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 stand-alone program unit that should have a class name and consist of two main parts: a property description and a 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

Declaring a variable by using the keyword var in the class definition creates a property of the class, also called the member property of the class.

Grammar:

Class class_name{    var $var _name;}

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:

<?phpclass person {    //Member attribute    var $name;    The name of the person    var $age;    Person's age    //person member say () method    function say () {        echo "My name is:". $this->name. " <br/> "; echo" my Age is: ". $this->age;    }}    The class definition ends//instantiates an object $P1 = new Person (),//assigns a value to the $p 1 object property $p1->name = "Zhang San";  $p 1->age = 20;//The Say () method in the calling Object $p1->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.

PHP Classes and objects

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.