Basic knowledge of object-oriented programming (OOP) in PHP

Source: Internet
Author: User

Object-oriented is not a technology, but a kind of thinking, is a problem-solving the most basic way of thinking! How to understand usage? OOP: Object-Oriented Programming

(Direct code description)

1. Example of an object-oriented basic concept demo:

1<?PHP2 Header("Content-type:text/html;charset=utf-8");3 //How do I instantiate a class?  For example: $newA = new A (); Using a class to get an object, the procedure is an instantiation of the class. The keyword new is complete.4 //How do I define a class? Example: Class a{} classes5 //How do I declare a property? For example: Public $age control modifiers for property declarations: 1.public 2.protected 3.private6 //How do I declare a definition method? For example: Public function User () {} Method Object member (property, method) accessor:->7 //How do I define Class constants? For example: const work = ' PHP '; Class constant Access Form: Class Name:: Constant name8 //Summary: Only three members in a class: 1. Property (variable) 2. Method (function) 3. Class constant (constant)9 classa{//classTen      Public  $name;//Properties One      Public  $age; A     ConstWork = ' php ';//class Constants -      Public functionUser$object){//Method -         Echo' Name: '.$object->name. ' Age: '.$object->age. ' Work '. Self::work;//inside a class, use the keyword self instead of the current class name the     } - } - $newA=NewA ();//Instantiate object ==> class a{} - $newA->name= ' Admin_a '; + $newA->age = 100; - $newA->user ($newA); +  A  at //How do I use $this? For example: $this represents the current object of the class that currently belongs, who is currently calling, who is the current object - //Summary: A property cannot be accessed directly within a method, it needs to find its object before it can be accessed, and a typical practice is to use the $this keyword within a method in place of the current object. - classb{ -      Public  $name; -      Public  $age; -     ConstWork = ' html '; in      Public functionUser () { -         Echo' Name: '.$this->name. ' Age: '.$this->age. ' Work '. Self::work;//$this Current Object to     } + } - Echo"<pre>"; the $newB=Newb (); * $newB->name= ' Admin_b '; $ $newB->age = 200;Panax Notoginseng $newB->user ($newB);

Running results such as:

Basic knowledge of object-oriented programming (OOP) in PHP

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.