PHP Class object


Object Oriented Programming(OOP) is a computer programming architecture. A basic principle of OOP is that a computer program is composed of a single unit or combination of objects that can function as subroutines. OOP achieves the three goals of software engineering: reuse, flexibility, and extensibility.

PHP 4.0 in the improved version of the OOP after the support. For small applications, the use of traditional procedural programming may 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 objects with the same attributes and services. It provides a unified abstract description of all objects belonging to the class, including two main parts: attribute and service. In the object-oriented programming language, the class is a separate program unit, it should have a class name and include attribute description and service description of the two main parts.


Object

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

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

About object-oriented programming involves a wide range of content, this tutorial only introduces the 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 enclosed in the {} symbol.

grammar:

class class_name {
    ...
}

Classes contain properties and methods.


Properties

By using the keyword var in a class definition to declare a variable, you create a property of the class, also called a member property of the class.

grammar:

class class_name {
    var $var_name;
}

For example, if the definition of a person's class, then the person's name, age, sex, etc. can be seen as a class of people attribute.


Method

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

grammar:

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

Application of the class

A definition of the attributes and methods of the class is a complete class, and can be included in a class which contains a complete processing logic. Use the new keyword to instantiate an object to apply the logic inside the class. Multiple objects can be instantiated 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 a member's properties or methods in a defined class, you can use the pseudo-variable $this. $this is used to represent the current object or the object itself.

example:

class Person {
    // The member attribute of the person
    var $name; // The name of the person
    var $age; // people's age

    // People members say() method
    function say() {
        echo "My name is: ". $this->name. "<br />";
echo "My age is: ". $this->age;
    }
} // End of class definition

// Instantiate an object
$p1 = new Person();
// Assign a value to the $p1 object property
$p1->name = "Joe Smith";
$p1->age = 20;
// Call the object's say() method
$p1->say();

Run the example and output:

My name is David
My age is 20

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




Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook