Classes and objects

Source: Internet
Author: User
Tags ming

In an object-oriented programming language, a class is an abstraction of an object that defines a description of the properties and methods of an object, which is an instance of a class that can only be used if it is instantiated.

Defining classes

In PHP, you define a class using the keyword class plus the class name, then wrap the class body with curly braces, and define the properties and methods of the class in the class body. The format of the class is as follows:

<?phpclass MyClass {    private $var;    public function fun()    {       // 。。。。    }}

MyClass is the name of the class, and the middle part of the two curly braces is the entire contents of the class. In previous versions of PHP5, the properties of the class were declared using the keyword VAR, whereas in later versions of PHP5 (including 5), the private, protected, and public keywords were introduced to define private, protected, and shared members, which would make the program more secure. Classes and the methods that define member functions are the same as those that define normal functions, and you can also restrict access to member functions by using the keyword private, protected, and public.
Attention:
A class, that is, the entire contents of a pair of curly braces, is in a code snippet, a "

<?php class MyClass{    //..?><?php    ../}?>

This format is not correct

Instantiating classes

It is not directly used after the class definition is complete, and the class needs to be instantiated, declaring the object. PHP uses the keyword new to declare an object. The format is as follows:
Object name = new class name (Var0, Var1, .... Varn)
When an object is instantiated, you need to specify parameters for the class's construction method.
For example:
Define a student class (student Class), define member variables in the class body, $name and respectively, to $age represent the student's name and age; __construct () is a constructor for the class that initializes the member variables of the class; the last definition Method Getnameandage () To output student information

<?php class Student{    Private $name;Private $age; Public  function __construct($name, $age)//define constructor method {        $this->name =$name;$this->age =$age; } Public  function getnameandage() {        return "Student".$this->name."This year".$this->age."Old"; }}$student=NewStudent ("Xiao Ming", the);Echo $student->getnameandage ();

The result of the operation is:
Student Xiao Ming 15 years old this year

Working with objects

After the class definition is complete, it needs to be instantiated before it can be used, and the object may invoke the public and public methods in the class, which are properties and methods that are decorated with the key. The calling method is as follows:
Object name-property; Invoking the properties of a class
Object name---method; Methods to invoke a class
As can be found in the above code, PHP calls the properties and methods of the class using the symbol "," rather than ".", unlike some other languages, beginners should be careful not to confuse.
For example:
Define the class book and define the title of the public member in the class, and the bookName public method is getBookName() used to get the book name. After instantiating the bookName book class, getBookName() the names of the books are printed in the page by properties and methods respectively.

<?php Class Book {     Public $bookName; Public  function __construct($bookName)//define constructor method {        $this->bookname =$bookName; } Public  function getbookname() {        return $this->bookname; }}$book=NewBook ("PHP from getting started to giving up");Echo $book->bookname;Echo "<br>";Echo $book->getbookname ();

The result of the operation is:
PHP from getting started to giving up
PHP from getting started to giving up
NOTE: If you declare a property or method in the class body as a private or protected member, running this instance again will prompt you for a message error. So from the above example, the PHP property can only invoke the class's public properties and public methods directly.

Member variables

A variable that is defined in a class becomes a member variable (it can also be a property or field). Member variables are used to hold information data, or to interact with member methods to implement a function.
The format for defining member variables is generally:
Variable declarator member variable name.
Description: A variable declarator can use either public,private,protected,static and final. (For detailed usage of these, the mini-series is being updated, please look forward to)
The access member variable and the Access member method are the same, as long as the member method is replaced by the member variable, in the format:
Object name--member variable
For example:
Declare a member variable in the MyClass class $class_name , and then declare two member methods for the variable $setClassName() and $getClassName() . Instantiate the MyClass class and pass the parameter "Hello PHP" for the class's constructor, and the final output

<?php class MyClass{     Public $class _name; function setclassname($name) {        $this->class_name =$name; } function getclassname(){        return $this->class_name; }}$c _book=NewMyClass ();$c _book->setclassname ("Hello PHP");Echo $c _book->class_name.' <br> ';Echo $c _book->getclassname ();

The result of the operation is:
Hello PHP
Hello PHP

member functions

functions defined in the class body are called member functions or member methods, mainly to implement a function, the method of defining member functions in php4.x is the same as the method of defining the normal function, while in php5.x, the permission modifier can be set for the member method, which can effectively improve the logic and security of the code. The syntax format for member functions of a class defined in php5.x is as follows:
Access rights modifier function member functions name (VAR0, var1, ..., Varn)
{
function body
}
For example:
Define the class MyClass and define the attributes in the class body $name , define the public methods setName() and the getName() values that are used to $name assign and get $name the attributes, respectively, as follows:

<?php Class MyClass{    Private $name; Public  function setName($name) {        $this->name =$name; } Public  function getName() {        return $this->name; }}$myClass=NewMyClass ();//Instantiate a class$myClass->setname ("Computer");Echo $myClass->getname ();

The result of the operation is:
Computer


Pure hand Play, the object-oriented detailed explanation, is still continuously updated, if there is no place, hope to correct.

Classes and objects

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.