PHP Object Tutorials

Source: Internet
Author: User

"Getting Started with PHP object-oriented (OOP) programming" 6. How to use members in an object qianyunlai.com posted: 2012-05-19 15:02 Category: PHP basic browse (280)

There are two types of members in a PHP object that are member properties, and one is a member method. We can declare the object by the $p 1=new person (); How to use the members of the object? To access members of an object, you use a special operator, "-" to accomplish access to the object members:

Object, Properties $p 1->name; $p 2->age; $p 3->sex;

Object, Method $p 1->say (); $p 2->run (); As in the following example:

<?php class Person {      // 下面是人的成员属性      var $name ;      // 人的名子      var $sex ;       // 人的性别      var $age ;       // 人的年龄      // 下面是人的成员方法      function say() { // 这个人可以说话的方法          echo "这个人在说话" ;      }      function run() { // 这个人可以走路的方法          echo "这个人在走路" ;      } } $p1 = new Person(); //创建实例对象$p1 $p2 = new Person(); //创建实例对象$p2 $p3 = new Person(); //创建实例对象$p3 // 下面三行是给$p1对象属性赋值 $p1 ->name = "张三" ; $p1 ->sex = "男" ; $p1 ->age = 20; // 下面三行是访问$p1对象的属性 echo "p1对象的名子是:" . $p1 ->name; echo "p1对象的性别是:" . $p1 ->sex; echo "p1对象的年龄是:" . $p1 ->age; // 下面两行访问$p1对象中的方法 $p1 ->say(); $p1 ->run(); // 下面三行是给$p2对象属性赋值 $p2 ->name = "李四" ; $p2 ->sex = "女" ; $p2 ->age = 30; // 下面三行是访问$p2对象的属性 echo "p2对象的名子是:" . $p2 ->name; echo "p2对象的性别是:" . $p2 ->sex; echo "p2对象的年龄是:" . $p2 ->age; // 下面两行访问$p2对象中的方法 $p2 ->say(); $p2 ->run(); // 下面三行是给$p3对象属性赋值 $p3 ->name= "王五" ; $p3 ->sex= "男" ; $p3 ->age=40; // 下面三行是访问$p3对象的属性 echo "p3对象的名子是:" . $p3 ->name; echo "p3对象的性别是:" . $p3 ->sex; echo "p3对象的年龄是:" . $p3 ->age; // 下面两行访问$p3对象中的方法 $p3 ->say(); $p3 ->run(); ?>

As you can see from the example above, only the members of the object will be accessed using the object---Properties, Object-by-method, and there is no second way to access the members of the object.

PHP Object Tutorials

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.