Results 1 to 1 of 1

Thread: เขียน PHP แบบ OOP: ตอนการใส่ attribute ของ class

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    11


    เขียน PHP แบบ OOP: ตอนการใส่ attribute ของ class

    ต่อไปนี้เราจะมาใส่รายละเอียดของ คลาสกันครับ

    การใส่ attribute ของ class

    จาก โพสต์เดิมครับ ผมได้สร้างคลาส human มาแล้ว ได้ มันมี function talkครับ คราวนี้เรามากำหนด คุณสมบัติ หรือ attribute ให้ human ครับ

    สิ่งที่ผมต้องการ คือ
    ชื่อ
    ส่วนสูง
    น้ำหนัก

    มาดูโค๊ดกันดีกว่า
    ##########################// File Human.php
    <?php
    class Human
    {
    // var เป็นการประกาศตัวแปร
    var $name ; // สร้างตัวแปรของคลาส Human ชื่อว่า name
    var $height; // สร้างตัวแปรของคลาส Human ชื่อว่า height
    var $weight; // สร้างตัวแปรของคลาส Human ชื่อว่า weight
    function showName()
    {
    return $this->name; // $this เป็นตัวแปรที่อ้างคุณสมบัติตัวมันเอง ในที่นี้ อ้างถึง ตัวแปร name
    }
    function showHeight()
    {
    return $this->height;
    }
    function showWeight()
    {
    return $this->weight;
    }
    }
    ?>


    // File: TestClient.php

    <?
    require("Human.php");
    $human = new Human();
    $human->name = "doing";
    $human->height= 160;
    $human->weight = 60;
    echo "Name: ".$human->name;
    echo "Height: ".$human->height;
    echo "Weight: ".$human->weight;

    ?>

    ######################################
    ผลลัพธ์

    Name: doing Height: 160 Weight: 60

    Ref: https://sites.google.com/site/oopinphp/
    Last edited by doing; 15-08-2010 at 09:34 PM.

Similar Threads

  1. Replies: 0
    Last Post: 12-08-2010, 02:02 PM
  2. Replies: 0
    Last Post: 17-03-2010, 10:12 AM
  3. Replies: 0
    Last Post: 26-06-2009, 01:58 AM
  4. Replies: 0
    Last Post: 12-06-2009, 11:39 PM
  5. Attribute Changer 5.3
    By akira in forum Window Application
    Replies: 0
    Last Post: 22-12-2007, 01:05 AM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •