What this article brings to you is about how to define a class in PHP. PHP definition of a class method, there is a certain reference value, the need for friends can refer to, I hope to help you.
Object-oriented Basic concepts
Consists of 3 parts:
Object-oriented analysis (oriented ANALYSIS,OOA)
Object-oriented designing (object oriented Design,ood)
Object-oriented programming (object oriented Programming,oop)
Definition of Class
A class is a collection of properties and methods "human, animal, plant"
property, which is a variable defined within a class, also known as a member property, member variable.
method, which is a function defined within a class.
What do you need to learn?
How do I define a class?
<?php//keyword class + class name class person () {}
How do I instantiate a class?
<?phpclass person () {}//object = keyword class name (); $person =new Man ();
How do I tune one of the methods in a class?
<?phpclass person () { function run () { echo "I'm Running"; }} $person =new person (); $person->run ();//Run Result: My method in the Run//object--Class
How do I tune variable information in a class?
<?phpclass person () {public $name = "Shanyun";} $person =new person (); Echo $person->name ();//Run Result: Shanyun//Object-class variable name (note there is no $)
Definition of Object
An object is a concrete instantiation of an entity
Relationship of classes and objects
What are the modifiers for a variable?
Public: Common, attributes can be used outside of the class
Protected: Protected, properties can only be used inside the class (if there is inheritance, it may be used inside the subclass)
Private: Privately, can only be used inside the current class, and cannot be used anywhere else
Note: The default is the public modifier when the method is not in front of it;
Three main features of object-oriented
Encapsulation, inheritance, polymorphism
Encapsulation, also known as information hiding, classes retain only limited interfaces and external connections. Know how to invoke a method without worrying about how to implement the details.
inheritance, derived classes automatically inherit properties and methods from the parent class, improving code reusability. Extends
Polymorphism, refers to a class of different objects call the same method can get different results. Enhanced flexibility and reusability of the system.
After-school assignments:
Define a student class Student
Define attributes: Number (ID), name (name), Gender (sex).
Define public call methods: Say () output I'm Zhang San, sex guy, number 004.