Step by step learning PHP (5) class and object _ PHP Tutorial

Source: Internet
Author: User
Learn PHP (5) classes and objects step by step. In this section, we will look at how to create a class and object in PHP. 1. creating a class in PHP is basically similar to creating a class in C # Java. let's talk a little bit about it and show you the most in this section, let's take a look at how to create a class and object in PHP.

1. create a class

In PHP, the creation of a class is basically similar to that in C #/Java. let's take a look at the simplest example:

The code is as follows:


Class People
{
Var $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}

$ P = new People ();
$ P-> SetName ("kym ");
Echo ($ p-> GetName ());
?>


Here, we will create a People class, which has four key points:

The first is that in PHP, the access attribute (or method) is used instead of the commonly used vertex operator (.), but instead->.

The second is in PHP, the method must be identified by a function, which is similar to Javascript.

The third point is that when we declare a variable, we need to use var, which is similar to Javascript.

The fourth point is that in PHP, there are three access modifiers, namely public, protected, and private, which are the same as C.

Here, we found that we can use $ p to directly access the $ name attribute, so we need to control it as follows:

The code is as follows:


Class People
{
Private $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}


In this case, the $ name attribute cannot be accessed from outside.

Do you still remember the variable functions mentioned above? Here we can also use variable functions to access objects:

The code is as follows:


Class People
{
Private $ name;
Public function GetName ()
{
Return $ this-> name;
}
Public function SetName ($ name)
{
$ This-> name = $ name;
}
}

$ P = new People ();
$ Get = "GetName ";
$ Set = "SetName ";
$ P-> $ set ("kym ");
Echo ($ p-> $ get ());
?>


2. static methods (attributes)

Declaring static methods in PHP (also called class methods) is very similar to that in C.

The code is as follows:


Class DataBase
{
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
DataBase: CreateConnection ();
?>


Similarly, the declaration of static attributes is the same.

The code is as follows:


Class DataBase
{
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>


3. class constants

In C #, we use const to identify constants, which is also true in PHP.

The code is as follows:


Class DataBase
{
Const AUTHOR = "kym ";
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo ("Success ");
}
}
Echo (DataBase: AUTHOR );
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>


4. access static variables

We know that static attributes (methods) belong to the class, while variables (methods) belong to the object, and classes exist before objects, so how can we access static variables (methods) in dynamic methods? In PHP, the self keyword is provided.

The code is as follows:


Class DataBase
{
Const AUTHOR = "kym ";
Static $ connectionString = "http: // 127.0.0.1 ";
Public static function CreateConnection ()
{
Echo (self: $ connectionString. "Success ");
}
}
Echo (DataBase: AUTHOR );
Echo (DataBase: $ connectionString );
DataBase: CreateConnection ();
?>

Bytes. 1. create a class in PHP. creating a class is basically similar to creating a class in C #/Java. let's talk a little bit about it and show you the most...

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.