PHP Object-oriented (i)

Source: Internet
Author: User
Tags access properties

I. Classes and objects

1, what is a class: class is a description of a class of things, is an abstract, conceptual definition. is a collection of objects with some of the same properties and functional behavior.

In object-oriented programming, a class should have a class name and include two main parts: the title of the property and the description of the function.

2. An object is an entity/instance used in the system to describe a customer's view of things.

3, the instantiation of the class result is the object, and the abstract of a class of objects is the classes. Class describes a set of objects that have the same attributes (attributes) and the same behavior (methods).

Ii. declarations of classes and creation of objects

1. Declaration of class

// declaring classes class class Name {  //Code (Properties  , Methods)...}

 Specification: Class name capitalization, camel-like syntax

2. Create objects

$ object Variable New class name ();

  

Iii. properties and methods of classes

Property (also known as a member variable of a class), a common property in a class.

Declaring a property must be decorated with one of the public/private/protected to define the access rights for the variable.

Method (a member function of a class), the functional behavior in a class, by declaring a function in a class definition, that is, the method of the class that was created.

  

1. Access by reference variable (object)

First instantiate an object, $variable = new ClassName (), and then use the operator "-" to complete access to the object member properties and member methods.

Access properties: Objects, properties. "$variable->name;"

Modify property: Object--property = new value. "$variable->name = ' newName ';"

Access method: Object---method "$variable->method ();"

2. Intra-class access

Internal Access with keyword $this

Access properties: $this-Properties; "$this->name;"

Modify Property: $this-property = new value; "$this->name = ' newName ';"

Access method: $this-to method; "$this->method ();"

For example:

<?PHPclassperson{ Public $name;//Defining Properties      Public $age;//Defining Properties      Public functionSay () {//Create a method        Echo' My name is '.$this->name. "And my age is".$this->age. " <br/> "; }      Public functionSetName ($name= ' wuming ') {//ways to change the Age property         $this->name =$name; }      Public functionSetage ($age=18){        $this->age =$age; } } $person 1=NewPerson ();//instantiating the person class $person 1->setname (' Xiaozu '); Calling Methods$person 1->setage (24); Echo  $person 1->say (); ?>

PHP Object-oriented (i)

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.