Home >Backend Development >PHP Tutorial >PHP Object-Oriented Journey: Classes and Objects_PHP Tutorial

PHP Object-Oriented Journey: Classes and Objects_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:44780browse

From an OOP perspective, languages ​​should not be distinguished. Whether it's C++, Java, .net or more object-oriented languages, as long as you understand the true meaning of OO, you can transcend languages ​​and let your thoughts jump easily. There is no longer any dispute about who is stronger among Java, .net, and PHP.

I hope this introduction to PHP5 object-oriented programming (OOP) will benefit beginners and enable more PHPers to start switching to OO programming.
Compared to PHP4, PHP5 has changed a lot in terms of object orientation. We will only introduce object-oriented in PHP5 environment. And we must change ourselves to follow the development of PHP5. If the code results are inconsistent in your environment, please confirm that your environment is PHP5.
We assume that the reader does not have any object-oriented knowledge. Even if you are hearing about OOP for the first time, you can still understand this article. But I hope you must have some knowledge about PHP.
We will use some examples later to gradually analyze the OO foundation of PHP5.
Object-oriented only solves two problems, code scalability and code maintainability.
I have to say that php is becoming more and more like Java.
Everything is Object: Everything is an object.
Object-oriented programming (OOP) thinking strives to make the description of things in computer language as consistent as possible with the original appearance of the things in the real world. Object-oriented language is closely related to our lives, and it is actually very simple to learn object-oriented language. The application is 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 behavioral characteristics of each object.
Class/Object: Class and object are the core concepts of the object-oriented method.
A class is a description of a type of thing, an abstract and conceptual definition;
An object is every individual of this type of thing that actually exists, so it is also called an instance. In a computer, it can be understood that a real memory area is created in the memory to store this object.
The process of creating an object is called object creation, also called instantiation.
Classes and objects in PHP5
Let’s create a basic class first.
The keyword class is used in PHP to define a class. Class names generally use the first character to be capitalized, and then the first character of each word is capitalized to facilitate reading.
view sourceprint?
1
2 class Person
3 {
4
5 }
6 $p = new Person();
7 echo $p;
8 ?>
In this way, we have our first PHP class.
We continue to use this class, use the new keyword to create objects, and use echo to print $p
We define a variable $p and use the new keyword to create a Person object.
Print variable $p, we see the output Object id #1, indicating that this is an object.
$p = new Person(); can also be written as $p = new Person;, but it is not recommended to use the latter method.
Properties in PHP5
Attributes: The data elements used to describe an object are called attributes of the object (also called data/state)
In PHP5, attributes refer to variables declared in class. When declaring a variable, it must be modified with one of public private protected to define the access rights of the variable.
•Public: Can be freely read and modified inside and outside the class.
•Private: Can only be read and modified within the current class.
•Protected: Can be read and modified in this class and its subclasses.
Use of attributes: Call the attribute of the object pointed to by the variable by referencing the -> symbol of the variable.
Call a property of the same object inside a method via the $this-> notation.
view sourceprint?
01
02 class Person{
03 public $name = "Gonn"; //Define public attribute $name.
04 public $age = 20; // Define public property $age;
05 }
06 $p = new Person(); // Create object
07 echo $p."
"; // Output object
08 echo "His name is ".$p->name; // Output the attributes of object $p $name;
09 echo "
";
10 echo 'His age is '$p->age; //Output the age attribute.
11 ?>
The program output result is:
view sourceprint?
1 His name is Gonn
2 His age is 24
The Person class has two attributes, $name and $age. After instantiation, use $p->name and $p->age to print out the content of the attributes.
Of course, you can not set the initial value when defining the attribute. In that case, no results will be printed.
Change the properties of the object, pay attention to lines 8 and 9 of code, and the changes in the output results. We see that the output attribute value has been changed.
view sourceprint?
01
02 // 1-4.php
03 class Person{
04 public $name = "NoName"; //Define public attribute $name.
05 public $age = 20; // Define public property $age;
06 }
07 $p = new Person(); // Create object
08 $p->name = 'Tom'; //Change name to Tom
09 $p->age = 25; // Change age to 25 years old.
10 echo "His name is ".$p->name; // Output the attributes of object $p $name;
11 echo "
";
12 echo 'His age is '.$p->age; //Output the age attribute.
13 ?>
Create a Person object and change the properties of this object. Name it and see its name. You are the god of the Person object in the machine. According to the rules you defined, this real Person object in memory is created, and it has properties that can be changed.
Now, we are the gods of the computer world, ready to create the world.
Private modified properties cannot be accessed outside the current object. Private attributes are set to hide data.
Hiding: refers to a protection mechanism for an object so that its properties or methods are not directly accessed by external programs.
view sourceprint?
1
2 // 1-3.php
3 class Person{
4 private $name = "Gonn"; //Define public attribute $name.
5 public $age = 20; // Define public property $age;
6 }
7 $p = new Person(); // Create object
8 echo "His name is ".$p->name; // Output the attributes of object $p $name;
9 ?>
Running this program will output:
view sourceprint?
1 Fatal error: Cannot access private property Person::$name in E:PHPProjectstest.php on line 9
Private attributes cannot be accessed externally. The benefits of this will be introduced later.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477751.htmlTechArticleFrom an OOP perspective, languages ​​should not be distinguished. Whether it’s C++, Java, .net or more object-oriented languages, as long as you understand the true meaning of OO, you can transcend languages ​​and let you...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn