Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Object-oriented example Analysis in php

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the php object-oriented example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

A preliminary study of php object-oriented

1. Class: a class is a collection of properties and methods is an abstract concept such as "people" and "cars" in life.

two。 Object: the object is specific things, such as a person called "Xiaoqiang" and a car called "Audi A7".

3. The relationship between a class and an object: a class is an abstract collection of something. The object is a concrete part of the class.

Example 1: create a Person class

Step 1: declare a Person class

The member attribute of the class Person {/ / Person class declares that the property of the class should be declared using the var keyword var $name; var $age; var $sex; / / Person class member method function say () {echo "this person speaks again";} function run () {echo "this person is walking";}}

Step 2: instantiate the object

/ / the new keyword creates an instance $person1=new Person (); / / creates an instance of the person class $person2=new Person ()

Step 3: object member assignment

/ / object property assignment (using->) / / object $person1 object property initialization assignment; $person1- > name= "Zhang San"; $person1- > age=25;$person1- > sex= "male"; / / object $person2 object property initialization assignment; $person2- > name= "Li Si"; $person2- > age=23;$person2- > sex= "female"

Step 4: access the object

/ / the following is the property echo "person1 object" in the access object $person1 object: ". $person1- > name,"

"; / output the value of $name in the $person1 object; the age of the echo" person1 object is ". $person1- > age,"

"; / output the value of $age in the $person1 object; the gender of the echo" person1 object is: ". $person1- > sex,"

"; / / output the value of $sex in the $person1 object; / / here is the method echo that accesses the object $person1 object"

"; $person1- > say (); echo"

"; $person1- > run (); echo"

"; / / the following is the property echo in the access object $person2 object"

"; echo" person2 object name is: ". $person2- > name,"

"; / output the value of $name in the $person2 object; the age of the echo" person2 object is ". $person2- > age,"

"; / output the value of $age in the $person2 object; the gender of the echo" person2 object is: ". $person2- > sex,"

"; / / output the value of $sex in the $person2 object

Example 2: create a phone class (complete)

/ declare a phone Phone class class Phone {var $Manuefatuures; var $color; var $Battry_capacity; var $Screen_size; function call () {echo "on the phone";} function message () {echo "sending text message";} function Play_music () {echo "music playing";} function photo () {echo "taking a picture";}} echo "

The object of a mobile phone

"; / / create an instance of the Phone object $phone1=new Phone (); $phone1- > Manuefatuures=" huawei p6 "; $phone1- > color=" black "; $phone1- > Battry_capacity=3000;$phone1- > Screen_size=" 360 instances 480 "; echo $phone1- > Manuefatuures."

"echo $phone1- > color."

"echo $phone1- > Battry_capacity."

"echo $phone1- > Screen_size."

"

Php object oriented (2) $this

This above is all the content of the article "object-oriented sample Analysis in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report