Learn more about the concept of PHP class _php tutorial

Source: Internet
Author: User
For example, a vehicle can define properties such as color, number of tires, manufacturer, model, and capacity, and define actions such as stop, forward, turn, and whistle. In OOP terminology, the specific definition of the nature and behavior of an entity is called a class.

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

Class is used to represent the actual things to be handled in the application. For example, if you are creating an application that manages a public library, you might want to include classes that represent books, magazines, employees, special events, customers, and other things that need to be managed. Each entity contains a set of properties and behaviors, which in OOP are called fields (field) and methods (method), and they define entities. The general syntax for class creation in PHP is as follows:

Copy CodeThe code is as follows:
Class Class_name
{
Field declarations
Method declaration
}

Create a class:
Copy CodeThe code is as follows:
Class Employee
{
Private $name;
Private $title;
protected $wage;

protected function Clockin () {
echo "Member $this->name clocked in". Date ("H:i:s");
}

protected function Clockout () {
echo "Member $this->name clocked out at". Date ("H:i:s");
}
}

This class, named Employee, defines 3 fields: name, title, and wage, and two methods are defined: Clockin (check-in) and Clockout (sign-off).

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.

instantiation of the class:
Copy CodeThe code is as follows:
Object = new Class_name ();

After instantiating an object, use the-operator to access the object's member properties and methods. Like what:
Copy CodeThe code is as follows:
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.
Copy CodeThe code is as follows:
Class Person {
People's member properties
var $name; Man's name
var $age; The age of the person

Person's member say () method
function say () {
echo "My name is:" $this->name. "
";
echo "My Age is:". $this->age;
}
}
End of class definition

$p 1 = new person (); Instantiate an Object
$p 1->name = "Gonn"; Assigning values to $p 1 object properties
$p 1->age = 25;
$p 1->say (); Call the Say () method in the object
?>

Program Run Result:
Copy CodeThe code is as follows:
My name is: gonn
My age is: 25

http://www.bkjia.com/PHPjc/325556.html www.bkjia.com true http://www.bkjia.com/PHPjc/325556.html techarticle For example, a vehicle can define properties such as color, number of tires, manufacturer, model, and capacity, and define actions such as stop, forward, turn, and whistle. In OOP terminology, the entity ...

  • 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.