object-oriented Programming in PHP: Ways to develop large PHP projects (i) (reprint)

Source: Internet
Author: User
Programming | object | Project PHP object-oriented Programming: Methods for developing large PHP projects (i)





Author: Luis Argerich translator: Limodou





This article describes object-oriented programming in PHP (Oop,object oriented programming). I will show you how to pass


uses some OOP concepts and PHP techniques to reduce coding and improve quality. 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 ()


$this->x= $v;


}


function GetX () {


return $this->x;


}


}


?>--------------------------------------------------------------------------------


Of course you can define your own preferences, but it's best to keep a standard, which is more effective.





data members are defined in a class using the "var" declaration, which is not typed until the data member is assigned a value. A data member can be


to be an integer, an array, an associated array (associative array), or an object.




The
method is defined as a function form in a class, and when you access a class member variable in a method, you should use $this->name, or a side


method, it can only be a local variable.





use the new operator to create an object:





$obj =new something;





then you can use the member function to pass:





$obj->setx (5);


$see = $obj->getx ();





in this example, the SETX member function assigns 5 to the member variable X (not the Class) of the object, and then Getx returns its value of 5.





You can access data members in the same way as $obj->x=6, which is not a good oop habit. I strongly recommend that you pass


methods are used to access member variables. If you consider a member variable to be
and use the method only through the object handle, you will be a

is a good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP.





inheritance is easy to implement in PHP as long as the Extend keyword is used.











--------------------------------------------------------------------------------


<?php





class Another extends something {


var $y;


function Sety ($v) {


$this->y= $v;


}


function GetY () {


return $this->y;


}


}





?>--------------------------------------------------------------------------------





Transferred from Phpbuilder.com














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.