ช่วยแก้ function PHP หน่อยครับ ใช่ public แล้วไม่ทำงาน

เริ่มโดย JumDaiDee, 30 ธันวาคม 2014, 00:25:44

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

JumDaiDee

พี่ๆ ครับช่วยดูหน่อยครับ

ผมเพิ่ม public แล้วเรียก function บนไม่ได้ครับ

ลองใส่ $this-> แล้วก็ยัง Error ครับ

มันเป็น function หา ห.ร.ม ครับ

อ้างถึงpublic function gcd($a, $b)
   {
       if ($a == 0 || $b == 0)
           return abs(max(abs($a), abs($b)));
       $r = $a % $b;
       return ($r != 0) ? gcd($b, $r) : abs($b);
   }

   public function gcd_array($array, $a = 0)
   {
       $b = array_pop($array);
       return ($b === null) ? (int) $a : gcd_array($array, gcd($a, $b));
   }

   echo gcd_array(array(50, 100, 150, 200, 400, 800, 1000)); // output 50
สินค้าออนไลน์ https://www.shoppook.com/product | โปรแกรมออนไลน์ : https://appnon.com

saparee


kororo56

public มันใช้สำหรับการประกาศ Visibility  ใน class ไม่ใช่หรือ

ossytong

public คือ การกำหนด Visibility ให้กับ Method ครับ
ซึ่ง Method คือ ส่วนการทำงานที่อยู่ใน Class ครับ

แต่จาก code ที่ยกว่า เรียกว่า function ไม่ต้องมีการกำหนด Visibility ครับ

JumDaiDee

อ้างถึงclass Test_Math {

       public function gcd($a, $b)
       {
           if ($a == 0 || $b == 0)
               return abs(max(abs($a), abs($b)));
           $r = $a % $b;
           return ($r != 0) ? gcd($b, $r) : abs($b);
       }

       public function gcd_array($array, $a = 0)
       {
           $b = array_pop($array);
           return ($b === null) ? (int) $a : gcd_array($array, gcd($a, $b));
       }
   }

   $mt = new Test_Math();

   echo $mt->gcd_array(array(50, 100, 150, 200, 400, 800, 1000)); // output 50

ผมลองใช่ class แล้วครับ แต่มันก็ยังไม่ทำงาน ส่วน Error ไม่ทราบมันเป็นหน้าขาว เฉยๆ เลยครับผม

ท่าให้ผมเดา มันเรียก function ไม่ได้ใช่หรือเปล่าครับ เพราะจาก class นี้ function ล่างเรียกใช้ function อีกทีครับ
สินค้าออนไลน์ https://www.shoppook.com/product | โปรแกรมออนไลน์ : https://appnon.com

nagis


function gcd($a, $b) {
    if ($a == 0 || $b == 0)
        return abs(max(abs($a), abs($b)));
    $r = $a % $b;
    return ($r != 0) ? gcd($b, $r) : abs($b);
}

function gcd_array($array, $a = 0) {
    $b = array_pop($array);
    return ($b === null) ? (int) $a : gcd_array($array, gcd($a, $b));
}

echo gcd_array(array(50, 100, 150, 200, 400, 800, 1000)); // output 50


ลองดู

Nomkhonwaan

class Test_Math {

    public function gcd($a, $b)
    {
        if ($a == 0 || $b == 0)
            return abs(max(abs($a), abs($b)));
        $r = $a % $b;
        return ($r != 0) ? $this->gcd($b, $r) : abs($b);
    }

    public function gcd_array($array, $a = 0)
    {
        $b = array_pop($array);
        return ($b === null) ? (int) $a : $this->gcd_array($array, $this->gcd($a, $b));
    }
}

$mt = new Test_Math();

echo $mt->gcd_array(array(50, 100, 150, 200, 400, 800, 1000)); // output 50


ถ้าต้องการอ้างอิง method ภายใน class ต้องใช้ $this ไม่อย่างนั้น compiler จะเรียกหา global function (ซึ้งไม่ได้ประกาศไว้ใน program) มันก็เลย error ครับ