Step by Step Learn PHP (5) Class and Object _php basics

Source: Internet
Author: User
Tags learn php
In this section, let's take a look at how to create a class and object in PHP.

1. Create Class

In PHP, create a class similar to the one in C#/java, gossip less, and show you the simplest example:
Copy Code code as follows:

<?php
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'll create a people class, here are four points to illustrate:

The first is that in PHP, access to properties (or methods) is not in the use of the dot operator (.) we use, but rather with->.

The second is in PHP, where the method needs to be identified with a function, which is similar to JavaScript.

The 3rd is that when we declare a variable, we need to use Var, which is very similar to JavaScript.

The 4th is in PHP, also has public, protected,private three and C # the same access modifier, no longer repeat.

Here, we find that we can use $p to access the $name property directly, then we need to control it, the following methods:
Copy Code code as follows:

class people
{
Private $name;
Public Function GetName ()
{
return $this->name;
}
Public Function SetName ($name)
{
$this->name= $name;
}
}

At this point, we can not access the $name properties outside the world.

Remember that we mentioned the variable function in the above? Here we can use the variable function to access the object's method:
Copy Code code as follows:

<?php
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 method (properties)

Declaring a static method (also known as a class method) in PHP is very similar to C #.
Copy Code code as follows:

<?php
Class DataBase
{
public static function CreateConnection ()
{
Echo ("Success");
}
}
Database::createconnection ();
?>

Similarly, declaring static properties is the same.
Copy Code code as follows:

<?php
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, as in PHP.
Copy Code code as follows:

<?php
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. Accessing static variables

We know that static properties (methods) belong to the class itself, and that the variables (methods) belong to the object itself, and that the class itself exists before the object, how do we access the static variable (method) in the dynamic method? In PHP, we provide the Self keyword.
Copy Code code as follows:

<?php
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 ();
?>

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.