Project This article describes object-oriented programming in PHP (Oop,object oriented programming). I'll show you how to reduce coding and improve quality by using some OOP concepts and PHP techniques. Good luck!
The concept of object-oriented programming:
different authors may say differently, but an OOP language must have the following:
abstract data types and information encapsulation
Inheritance
polymorphic
is encapsulated in PHP through a class:---------------------------------------------------<?php
class Something {
//In an OOP class, the first character is usually uppercase
var $x;
function SetX ($v) {
The
//method starts as a lowercase word, and then uses uppercase letters to separate the words, such as Getvalueofarea ()
The object of the
"Another" class now has all the data members and methods of the parent class (something), plus its own data members and methods.
You can use
$obj 2=new something;
$obj 2->setx (6);
$obj 2->sety (7);
PHP does not now support multiple inheritance, so you cannot derive new classes from two or more than two classes.
you can redefine a method in a derived class, and if we redefine the Getx method in the "Another" class, we can't use the Getx method in "something". If you declare a data member with the same name as Kippe in a derived class, it will "hide" the data members of the base class when you process it.
you can define constructors in your class. A constructor is a method that has the same name as a class name and is invoked when you create an object of a class, for example:
The
constructor automatically assigns 6 to the data variable x. Constructors and methods are common PHP functions, so you can use the default parameters.
function Something ($x = "3", $y = "5")
Then:
$obj =new Something (); X=3 and Y=5
$obj =new Something (8); X=8 and Y=5
$obj =new Something (8,9); X=8 and Y=9
The default parameter uses C + +, so you can't ignore the value of Y, and give X a default parameter, which is assigned from left to right if
the parameters passed in are less than the required parameters, they will use the default parameters.
when an object of a derived class is created, only its constructors are invoked, and the constructor of the parent class is not invoked, if you want to call the base
class constructor, you must display the call in the constructor of the derived class. You can do this because the methods of all the parent classes in a derived class are
A good mechanism for
oop is to use abstract classes. An abstract class is not instantiated and can only be supplied to a derived class as an interface. Designers usually
uses abstract classes to force programmers to derive from a base class, which ensures that the new class contains some expected functionality. There is no standard method in PHP,
but:
If you need this feature, you can define the base class and add a "die" call after its constructor so that the base
The
class is not instantiated and is now preceded by a "die" statement after each method (interface), so if a programmer does not have
in a derived class
overrides method, an error is raised. And because PHP is untyped, you may need to verify that an object is derived from your base class
class, add a method in the base class to the identity of the real class (return some identity id) and validate
when you receive an object parameter
this value. Of course, if an evil-bad programmer overrides this method in a derived class, this method will not work, but the general
The
problem is more of a lazy programmer than an evil one.
of course, it's good to make the base class impossible for programmers to see, just print the interface to do their job.
there is no destructor in PHP.
Overloading (unlike overlay) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same
's name, but there are different numbers or types of arguments (this depends on the language). PHP is a loosely typed language, so the type overload does not
works, however, overloading does not work by varying the number of parameters.
sometimes overloading constructors in OOP is great, so you can create objects in different ways (passing a different number of arguments). in PHP
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.