One of the learning of object-oriented programming in PHP

Source: Internet
Author: User
Inherited
1. Class members defined within the parent class can save programming time and cost without repeating definitions in subclasses
2, the subclass of the same parent class has the same class members as the parent class definition, so the external code can be treated equally when calling them.

3, subclasses can modify and adjust the class members of the parent class definition

 
  weight;
	}
	Public Function Setweight ($w)
	{
		$this->weight = $w;
	}
}

Class Dog extends Animal
{
	/**
	* Sub class new method
	/Public Function Bark ()
	{
		echo wang~~ Wang~~~ ";
	}
}

$myDog = new Dog ();
$myDog->setweight (a);
echo "Mydog ' s weight is". $myDog->getweight (). '
'; $myDog->bark (); ? >


Access control
1, object-oriented three kinds of permissions
(1) Public: A total of class members, can be visited in the classroom place
(2) Protected: A protected class member that can be accessed by itself and its subclasses
(3) Private: A privately-owned class member that can only be accessed by itself.

static keywords (statics)
1, static properties to save the common data of the class
2, static methods can only access static properties
3, static members do not need to instantiate an object to access
4. The internal of a class can access its own static members through self or static keywords
5, through the parent keyword can access the static members of the class
6, you can access static members outside the class definition by the name of the class


Final member
1. For classes that do not want to be inherited by any class, you can add final keyword before class
2. For methods that do not want to be overridden by a quilt, you can add the final keyword before the method definition


Data access
1, parent keyword can be used to access the parent class in the Quilt class rewrite method
2. The Self keyword can access the member methods of the class itself, or it can be used to access its own static members and class constants, not to access the properties of the class itself, and to use constants without adding the ' $ ' symbol before the constant name
3. The static keyword is used to access statically members defined by the class itself, and a ' $ ' symbol is required before the property to access the static property.



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.