An example of understanding PHP Object-Oriented Programming (OOP), Object-Oriented Programming (oop)

Source: Internet
Author: User

An example of understanding PHP Object-Oriented Programming (OOP), Object-Oriented Programming (oop)

 

 

<? Php

Class Person {

// Declare a new public variable$ NameCan be accessed by classes in any package
Public $ name; public $ sex; public $ age;

// Declare a method of this class function say () {echo "Hello! <Br/> "; echo" My name is :". $ this-> name. "<br/>"; echo "My gender is :". $ this-> sex. "<br/> ";}}
// Declare the new Student class as the Child class of Person Student extends Person {public $ school; function study (){
// Parent: can be used to call the member methods defined in the parent class.
Parent: say (); echo "My age is:". $ this-> age. "<br/> I am going to school.". $ this-> school. ";}}
// You only need to define P1 as a Student class $ p1 = new Student ();

// Assign $ P1-> name = "zhangsan"; $ p1-> sex = "male" to the name, sex, age, and school attributes of p1 "; $ p1-> age = "18"; $ p1-> school = "XX school"; $ p1-> study ();?>

 

 

The browser output result is as follows:

Hello!
My name is: Zhang San
My gender is: Male
My age is: 18
I am going to school at XX School.

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.