PHP Object-oriented tour: Classes and Objects _php tutorial

Source: Internet
Author: User
Tags php class
From the perspective of OOP, language should not be differentiated. Whether it's C + +, Java, or even more object-oriented languages, as long as you know the true meaning of oo, you can cross the language and make your mind jump easily. There is no dispute between Java,. NET, and PHP who is strong or weak. Hopefully, this introduction to PHP5 object-oriented programming (OOP) can benefit beginners, allowing more phper to start turning to OO programming processes. Relative php4,php5 have changed a lot in terms of object-oriented. We will only introduce object-oriented in the PHP5 environment. And 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 are the first to hear OOP, you can read this article. But I hope you have some knowledge of PHP. In the following we will use some examples to gradually analyze the oo basis of PHP5. Object-oriented only solves two problems, the extensibility of code, the maintainability of code. I have to say that PHP is more and more like Java. Everything is object: everything is objects. The object-oriented programming (OOP) idea tries to make the description of things in computer language as consistent as possible in the real world. Object-oriented language and our life is figured out, object-oriented language learning is actually very simple. In the application more in line with our life logic. Class is used to describe an object: The class describes the data that each object should include, and the class describes the behavior characteristics of each object. Class/object: Classes (Class) and objects (object) are the core concepts of object-oriented methods. A class is a description of a kind of thing, an abstract, conceptual definition; An object is an individual of the actual existence of such a thing, and thus is also called an instance (instance). In the computer, it can be understood that an area of memory in which a real existence is created is stored in this object. The process of creating an object is called creating an object, also known as instantiation. Classes and objects in PHP5 we first set up a basic class. PHP uses the keyword class to define a class. The naming of a class is generally capitalized with the first character, and then the first character of each word is concatenated in a convenient way to read. View Sourceprint?1 In this way, we have the first PHP class. We continue to use this class, create an object using the New keyword, print $p with echo we define a variable $p, and use the New keyword to create a person object. To print the variable $p, we see the output object ID #1 hint that this is an object. $p = new Person (), can also be written as $p = new person, but is not recommended in the latter way. Attribute properties in PHP5: A property (also known as data/state) that describes an object's data element called an object in PHP5, which refers to a variable declared in class. When declaring a variable, it must be decorated with one of the public private protected to define the access rights for the variable. Public (public): It is free to read and modify inside and outside the class. Private (private): can only be read and modified within the current class. protected (Protected): ability to read and modify in subclasses of this class and class. Use of a property: a symbol called by a reference variable that points to the property of the object. Invokes the properties of the same object within the method, using the $this-to symbol. View SOURCEPRINT?01 "; Output Object echo "his name is". $p->name; The properties of the output object $p $name; echo "
"; Echo ' His age is ' $p->age; Outputs the Age property. One?> program output is: View sourceprint?1 his name is Gonn 2 His age is the two attribute of the person class, $name and $age, after instantiating, using $p->name and $p->age to print out The contents of the property. Of course, you can not set the initial value when the property is defined, so that you cannot print any results. Change the properties of the object, note 8 lines and 9 lines of code, and change the output result. We see that the value of the output property has been changed. View SOURCEPRINT?01 name = ' Tom '; Change the name to Tom $p->age = 25; The age of Change is 25 years. Echo "His name is". $p->name; The properties of the output object $p $name; echo "
"; Echo ' his age is '. $p->age; Outputs the Age property. ?> create an object of person and change the properties of this object. Name it, and look at its name. You are the god of this person object in the machine, and according to the rules you define, this real in-memory person object is created, and it has properties that can be changed. Now, we are the god of the computer world, ready to create the world. The Private decorated property is not accessible outside the current object. The private property is set to hide the data. Hide: Refers to an object's protection mechanism, so that its properties or methods are not directly accessed by external programs. View Sourceprint?1 Name The properties of the output object $p $name; 9?> Run this program will output: View sourceprint?1 Fatal error:cannot Access private Property Person:: $name in E:\PHPProjects\test.php On line 9 Private properties cannot be visited externally, the benefits of doing so are described later.

http://www.bkjia.com/PHPjc/477751.html www.bkjia.com true http://www.bkjia.com/PHPjc/477751.html techarticle from the perspective of OOP, language should not be differentiated. Whether it's C + +, Java, whether it's a. NET or more object-oriented language, as long as you understand the true meaning of oo, you can cross the language and let you ...

  • Related Article

    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.