PHP Object-oriented basics (1)

Source: Internet
Author: User

Preface
This is the first personal blog I wrote to the blog community, and I will be here to record what I have learned. Know that there are 5 months less than the time to face the dilemma of finding a job, but I still want to insist on having time to write something. While writing this preface, I feel I can watch other knowledge, but I still want to persist, I believe there will be a different receipt. Quote this morning reading article here to inspire you and me along the It road, as to why you need some encouragement, perhaps you have had before, perhaps the future you will have such confusion: Where should I go? And why Shuold I go? At least the present self is no longer so confused. "Embarrassment can only make people do nothing, so long as they pay attention to the most basic things, everything else will follow." Just do it! ", with June mutual encouragement.
  
1. What is a constructor function?
A constructor is a method that executes when a given class or object is initialized (instantiated) and is typically represented by _construct (), which is called when a new object (instance) of the class is created in memory using the new operator, and the constructor does not need to be referenced. It is automatically referenced and executed at new.
Here's an example:
<?php
class person{
private $name;
private $age;
function _construct ($name, $age) {//constructor
this->name = $name;
this->age = $age;
}
function Personal_info () {
echo "My name is $this->name, I am $this->age year old." \ n ";
}
function _destruct () {//destructor
echo "I'm here to release the resources!" ";
}
}
$p 1 = new person ("small snail", 25);//An object of the new person class P1
$p 1->personol_info ();//Call the Personal_info () method of the person class
?>

2. What is a destructor?
In the example above, we define a function _destruct () at the end of the person class, which is the destructor, and after the script is executed, all objects will automatically call the destructor to clean up the resource.
Of course, you can also use the unset () function to eliminate objects, but generally do not display the call unset () function, which is typically used to save memory.

3. What is the difference between private and protected before member variables?
Private is used to define a member variable that is privately defined, and can only be visible in methods of this class, by using the member variables of the class that the private defines; protected is used to define a protected class, a member variable of a class defined by protected, and can only be visible in methods of this class or subclass or parent class.

4. Inheritance and overloading of classes
first, let's look at an example:
<?php
class person{
protected $name;
protected $age;
function _construct ($name, $age) {
$this->name = $name;
$this->age = $age;
}
function Personal_info () {
echo "My name is $this->name, I am $this->age year old." ";
}
function _destruct () {
echo "I'm here to clean up the memory drops!" ";
}
}
Class man extends person{//extends for inheriting classes
protected $height;
function _construct ($name, $age, $height) {
parant::_construct ($name, $age, $height);//The function of the parent class can be called from the child class through the parent:: struct
$this->height = height;
}
function Personal_info () {
Parent::p ersonal_info ();
echo "My height is $this->height." \ n ";
}
function _destruct () {
parent::_destruct ();
}

}
$man 1 = new man ("small snail", 25,180);
$man 1->personal_info ();
$p 2 = new person ("old snail", 50);
$p 2->personal_info ();
?>

PHP uses the keyword extends to inherit a class, PHP does not support multiple inheritance, that is, a class can have only one parent class.

  When a method inherited from a parent class does not meet the requirements of a subclass, the inherited class can be overridden, which is called overwriting or overloading. function overloading in PHP differs from C + + in that C + + is a function with the same function name, but with different parameter types.

when a method in a class is marked "final", the method cannot be overloaded.

Final function Personal_info () {
...//code
}

PHP Object-oriented basics (1)

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.