<?php
/*
* 1. Access to members in an object (in an object's internal methods, access other methods and member properties in this pair)
* 2. There is a $this keyword in the methods in the object by default, which represents the object that calls this method.
*
* Constructing method
*
* 1. It is the "first" and "automatically called" method after the object is created
*
* 2. Definition of constructor method, method name is a fixed,
* In php4: The same method as the class name is the constructor
* In php5: The constructor is selected to use the magic method__construct(). All classes declare constructors use this name
* Advantages: When changing the class name, the constructor does not need to be changed
* Magic method: Write a magic method in the class, and the corresponding functions of this method will be added
* The method names are all fixed (all provided by the system), and they are not defined by themselves.
* Every magic method is a method that is automatically called in order to complete a certain function at different moments.
* Different magic methods have different calls
* All methods start with __
* __construct(); __destruct(); __set();......
*
* Function: Initialize member attributes;
*
*
* Destruction method
*
* 1. The last "automatic" method called before the object is released
* Use garbage collector (java php), and C++ manually releases it
*
* Function: Close some resources and do some cleaning work
*
* __destruct();
*
*/
class Person{
var $name;
var $age;
var $sex;
//Constructor method in php4
/*function Person()
{
//Every time an object is declared will be called
echo "1111111111111111";
}*/
//Constructor method in php5
function __construct($name,$age,$sex){
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
function say(){
//$this->name;//Accessing of members in the object uses $this
echo "My name: {$this->name}, my age: {$this->age}<br>"
}
function run(){
}
function eat(){
}
//Destruction method
function __destruct(){
}
}
$p1=new Person("zhangsan",25,"male");
$p2=new Person;
$p3=new Person;