How do I define a class in PHP? A method of defining a class in PHP

Source: Internet
Author: User
Tags php definition
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

    1. Define attributes: Number (ID), name (name), Gender (sex).

    2. Define public call methods: Say () output I'm Zhang San, sex guy, number 004.

Related Article

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.