PHP object-oriented journey: Class and object

Source: Internet
Author: User
From the perspective of OOP, we should not differentiate languages. Whether it is C ++, Java, or. net also has more object-oriented languages. as long as you understand the true meaning of OO, you can jump across languages and make your thoughts jump easily. There is no dispute over who is strong or weak between Java,. net, and PHP. Xi... "/> <scripttype =" text/javascript "src =" htt from the perspective of OOP, the language should not be differentiated. Whether it is C ++, Java, or. net also has more object-oriented languages. as long as you understand the true meaning of OO, you can jump across languages and make your thoughts jump easily. There is no dispute over who is strong or weak between Java,. net, and PHP. I hope this article introduces PHP5 object-oriented programming (OOP) to benefit beginners and help more PHPer start to turn to the OO programming process. Compared with PHP4, PHP5 has changed a lot in object-oriented aspects. We will only introduce the object orientation in the PHP5 environment. We must change ourselves to follow the development of PHP5. If the code results are inconsistent in your environment, make sure your environment is PHP5. We assume that the reader does not have any object-oriented knowledge. even if you have heard of OOP for the first time, you can read this article. But I hope you must have some knowledge about PHP. We will use some examples later to analyze the OO basics of PHP5. Object-oriented only solves two problems: code scalability and code maintainability. I have to say that php is more and more like Java. Everything is Object: Everything is an Object. The idea of object-oriented programming (OOP) tries to make the description of things in computer languages as consistent as possible with that in the real world. Object-oriented language is quite simple to learn from our daily lives. The application is more in line with our life logic. Class is used to describe an Object: Class describes the data that each Object should contain, and Class describes the behavior characteristics of each Object. Class/Object: class and object are the core concepts of object-oriented methods. A class is an abstract and conceptual definition of a class of things. an object is an entity of a class of things that actually exists. Therefore, it is also called an instance ). In a computer, it can be understood that this object is stored in a memory region created in the memory. The process of creating an object is called object creation and instantiation. In PHP5, we first create a basic class for classes and objects. Use the keyword class in PHP to define a class. Class naming generally uses uppercase letters, and then each word is connected in uppercase to facilitate reading. View sourceprint? 1 In this way, we have the first PHP class. We will continue to use this class, use the new keyword to create an object, use echo to print $ p, we define a variable $ p, and use the new keyword to create a Person object. Print the variable $ p. we can see that the output Object id #1 prompts that this is an Object. $ P = new Person (); you can also write $ p = new Person;. However, this method is not recommended. Attribute in PHP5: the data element used to describe the object is called the object attribute (also known as the data/status). in PHP5, the attribute refers to the variable declared in class. When declaring a variable, you must use one of public private protected to define the variable access permissions. • Public: it can be read and modified freely outside the class. • Private: it can only be read and modified inside the current class. • Protected: it can be read and modified in the subclass of this class and class. Use of attributes: Call a variable to point to an object's attributes by referencing the variable's-> symbol. Call the attributes of the same object through the $ this-> symbol in the method. View sourceprint? 01 "; // Output object 08 echo" his name is ". $ p-> name; // attribute of output object $ p $ name; 09 echo"
"; 10 echo 'his age is '$ p-> age; // output age attributes. 11?> The program output result is: view sourceprint? 1 His name is Gonn 2 his age is 24 Person class has two attributes, $ name and $ age. After instantiation, use $ p-> name and $ p-> age to print the attribute content. Of course, you can not set the initial value when defining the attribute. in this case, no results will be printed. Change the attributes of an object. pay attention to the 8 and 9 lines of code and the changes in the output results. We can see that the output property value has been changed. View sourceprint? 01 Name = 'Tom '; // change the name to Tom 09 $ p-> age = 25; // change the age to 25. 10 echo "his name is ". $ p-> name; // attributes of the output object $ p $ name; 11 echo"
"; 12 echo 'his age is '. $ p-> age; // output age property. 13?> Create a Person object and change its attributes. Name it and view its name. You are the God of the Person object in the machine. according to your defined rules, the Person object in the real memory is created and has the changeable attributes. Now, we are the God of the computer world, ready to create the world. Private attributes cannot be accessed outside the current object. Private properties are set to hide data. Hiding: a protection mechanism of an object that prevents its attributes or methods from being directly accessed by external programs. View sourceprint? 1 Name; // The $ name of the output object $ p; 9?> Running this program will output: view sourceprint? 1 Fatal error: Cannot access private property Person: $ name in E: \ PHPProjects \ test. php on line 9 private attributes cannot be accessed externally. the benefits of doing so will be described later.

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.