A simple example of PHP object-oriented programming

Source: Internet
Author: User

<?phpdate_default_timezone_set ("PRC");

/*
* 1. Static properties are used to hold the class's public data
* 2. Static methods can only access static properties
* 3. Static members do not need to instantiate objects to be accessible
* 4. The inside of a class can access its own static members through the self or the static keyword
* 5. Static members of the parent class can be accessed via the parent keyword
* 6. You can access static members outside the class definition through the name of the class
*
*/
Class human{
Public $name;
protected $height; Only itself and subclasses can access
Public $weight;
Private $isHungry = true; Cannot access the Quilt class

public static $sValue = "Static value in Human class". \ n ";

Public function Eat ($food) {
echo $this->name. "' S eating "." ' $food "." \ n ";
}

Public Function info () {
echo "HUMAN:". $this->name. ";". $this->height. ";". $this->ishungry. " \ n ";
}
}
Class animal{

}


The definition of a class starts with the keyword class, followed by the name of the class, which is usually capitalized with the first letter, beginning and ending in brackets
Use the extends keyword in PHP to represent the class's inheritance, followed by the class name of the parent class
PHP can only follow the class name of a class after extends, which is the single-inheritance principle in PHP
Class Nbaplayer extends Human
{
Public $name = "Jordan"; Defining properties
Public $height = "198cm";
Public $weight = "98kg";
Public $team = "Bull";
Public $playernumber = "23";

Private $age = "40"; Private class members can only be accessed internally

Static properties are defined by adding the static keyword after the access control keyword
public static $president = "David Stern";
Static methods add the Static keyword after the access control keyword when you define it
public static function Changepresident ($NEWPRESDT) {
When using static members in a class definition, use the self or Static keyword followed by: operator to
Note that when you access the static member property,:: After you need to follow the $ symbol
Self:: $president = $NEWPRESDT;
Use the parent keyword to access static members in the parent class
Echo Parent:: $sValue. " \ n ";
}

constructor, which is called automatically when an object is instantiated
function __construct ($name, $height, $weight, $team, $playernumber)
{
echo "in Nbaplayer constuctor\n";
$this->name = $name; $this is a pseudo-variable inside PHP that represents the object itself, and can access the properties and methods of the object in a $->this way
$this->height = $height;
$this->weight = $weight;
$this->team = $team;
$this->playernumber = $playernumber;
echo $this->height. " \ n ";
}

Destructors, which are called automatically at the end of program execution
Destructors are often used to clean up resources used by the program. For example, if the program uses a printer, you can release the printer resources in a destructor
function __destruct ()
{
echo "destroying". $this->name. " \ n ";
}

Defining methods
Public Function Run ()
{
echo "running\n";
}

Public Function jump ()
{
echo "jumping\n";
}

Public Function Dribble ()
{
echo "dribbling\n";
}

Public Function Shoot ()
{
echo "shooting\n";
}

Public Function Dunk ()
{
echo "dunking\n";
}

Public Function Pass ()
{
echo "passing\n";
}
Public Function Getage () {
echo $this->name. "' S age is ". ($this->age-2). " \ n ";
}
}
Class-To-object instantiation
The class object is instantiated with the keyword new, followed by the name of the class and a heap of parentheses
$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat", "6");
The member properties in the object are accessed by using the symbol
To access static properties outside the class definition, you can access static members of the class by using the class name plus: operator:
echo Nbaplayer:: $president. " Before change "." \ n ";
Nbaplayer::changepresident ("Aadam siver");
echo Nbaplayer:: $president. " \ n ";
echo Human:: $sValue. " \ n ";
echo "Jordan:" $jordan->president. " \ n ";
echo "James:" $james->president. " \ n ";
echo $jordan->name. " \ n ";
echo $jordan->getage ();
$jordan->info ();
$jordan->eat ("Apple"); Properties and methods defined in the parent class can be accessed directly on the object in the child class
The member methods in the object are accessed by using the symbol
$jordan->dribble ();
$jordan->dunk ();
$jordan->jump ();
$jordan->pass ();
$jordan->run ();
$jordan->shoot ();
//
Each time an object is instantiated with new, the constructor is called with the argument list following the class name
$james = new Nbaplayer ("James", "203cm", "120kg", "Heat", "6");
echo $james->name. " \ n ";


By setting the variable to null, you can proceed to the call of the destructor
The destructor is triggered when the object is no longer in use
$james 1 = $james;
$james 2 = & $james;
$james 2 = null;
$james 1 = null;
echo "From now on James would not be used.\n";
?>

A simple example of PHP object-oriented programming

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.