About getting started classes

Class Description:

(1) class is a keyword, it indicates that this is a class that can not be modified

(2) public $ name, is a member attribute, when we variable {} class definition defined, it is a member of the attribute

(3) public access modifier is, he is used to control the member properties (variables) of the scope of access, in addition there are two public, protected and private, we will detail later

(4) $ cat1 = new Cat, this is called to instantiate an object, $ cat1 is an object, new is a keyword representing the new to create an object

(5) $ cat1-> name = 'white'; denotes to assign attribute member, -> symbol called an object operator

(6) When we need to look at the situation of an object, we may output the object by var_dump

The basic syntax class:

// definition of class 
// class name naming large hump CatName 
class Cat {
     // define properties 
    // initial value of the object property to be, if not to the initial default values for the null 
    public  $ name = 'Garfield' ;
     public  $ Age ;
     public  $ Color ;
     public  $ Food ;
     public  function ACT () {
             echo "and mouse" ; 
        } 
} 
// create corresponding object instance of the class by 
$ CAT1 = new new Cat;
 // to the subject assignment 
$ CAT1 -> name = 'Persian' ;
 $ CAT1 -> Age =. 3 ;
CAT1 $ -> Color = 'white' ;
 $ CAT1 -> Food = [ 'mouse', 'fish', 'cat' ];
 // method call object 
$ CAT1 -> ACT ();

 

 

Related categories:

(1) a plurality of class objects can be created

(2) the identifier # encode different objects, is not the same, an identifier assigned by the system when creating an object, assigned sequentially numbered

 Construction method:

           Explanation

Access modifier (1) constructor can be public, protected, private, under normal circumstances is public, the default is public

(2) __construct keyword can not be modified, __ _ underscore two.

(3) a constructor does not return value, i.e. there is no return, even with the return does not take effect

(4) A constructor is a system call, the programmer can not display the call

Quick Start Case:

class the Person {
     public  $ name ;
     public  $ Age ;
     public  $ Food ;
     // constructor parentheses $ name, $ age, $ food parameter represents a 
    public  function the __construct ( $ name , $ Age , $ Food ) {
         // $ this represents the current object, who calls who points to 
        // tell the incoming parameters assigned to the member properties 
        $ this -> name = $ name ;
         $ this -> Age = $ Age ;
         $ this -> Food = $ Food ; 
    } 
} 
// let the system automatically call the constructor to create an object p1 $ 
$ p1= New new the Person ( 'John Doe', 26, [ 'burger', 'duck']);

 Precautions:

  ① When you create a new object class, the system will automatically call the constructor of the class to complete the initialization of a new object

  ② the role of a constructor is to complete initialization tasks for member properties, rather than creating the object itself. So after the oop programming, the need for member properties are initialized, then the code in the constructor to

  ③ In one class, the constructor has one and only one, if a plurality of definitions, will be given.

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/rickyctbur/p/11031074.html