สอบถามการสืบทอดคลาส JFormFieldList ของ Joomla 4 กับ joomla 5

ถามตอบ ปัญหาการพัฒนา Extension ไม่ว่าจะเป็น Module plugin Component หรือ แม้แต่ template การปรับแต่งโค้ด Joomla ต่างๆ ทุกเวอร์ชั่น 1.5 2.5 หรือ 3.x

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
Thanapoom1514
PHP VIP Members
PHP VIP Members
โพสต์: 4329
ลงทะเบียนเมื่อ: 04/07/2022 9:46 am

สอบถามการสืบทอดคลาส JFormFieldList ของ Joomla 4 กับ joomla 5

โพสต์ที่ยังไม่ได้อ่าน โดย Thanapoom1514 »

สอบถามการสืบทอดคลาส JFormFieldList ของ Joomla 4 กับ joomla 5
ต้องการให้มันรอบรับทั้ง joomla 3 และ joomla 4 กับ 5 ครับ
ของ joomla 3 จะใช้ JFormFieldList ซึ่งโค้ดนี้เป็นปลั๊กอินใส่วนของฟอร์มจะ custom ฟิลด์ครับ

โค้ด: เลือกทั้งหมด

class JFormFieldGetComponent extends JFormFieldList
{
...
}
แต่ของ joomla 4 และ 5 ใช้ ListField ใช่ไหมครับ

โค้ด: เลือกทั้งหมด

use Joomla\CMS\Form\Field\ListField;
class JFormFieldGetComponent extends ListField
{
 ...
}
แต่ถ้าต้องการให้มันรอบรับทั้ง joomla 3, joomla 4 และ joomla 5 สามารถทำยังไงได้บ้างครับ
:gfb:
ภาพประจำตัวสมาชิก
Thanapoom1514
PHP VIP Members
PHP VIP Members
โพสต์: 4329
ลงทะเบียนเมื่อ: 04/07/2022 9:46 am

Re: สอบถามการสืบทอดคลาส JFormFieldList ของ Joomla 4 กับ joomla 5

โพสต์ที่ยังไม่ได้อ่าน โดย Thanapoom1514 »

ลองสอบถามใน ChatGPT คำตอบแบบนี้
ที่สอบถาม
In the case where the class inheritance name is different for each version in Joomla, what are the guidelines?
คำตอบ
Use conditional statements or version-specific checks to determine which class name or code to use based on the Joomla version. This can be done using JVERSION or other version checking mechanisms.

โค้ด: เลือกทั้งหมด

if (version_compare(JVERSION, '3.0', '>=') && version_compare(JVERSION, '4.0', '<')) {
    // Joomla 3.x code
    class MyClass extends Joomla3Class {
        // class implementation
    }
} elseif (version_compare(JVERSION, '4.0', '>=')) {
    // Joomla 4.x code
    class MyClass extends Joomla4Class {
        // class implementation
    }
}

มีวิธีที่ไม่ต้องเขียนเงื่อนไขและ 2 คลาสในคำตอบของ ChatGPT บ้างไหมครับ
:gfb:
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 47799
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: สอบถามการสืบทอดคลาส JFormFieldList ของ Joomla 4 กับ joomla 5

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

โค้ด: เลือกทั้งหมด

eval("class van extends $parent_class {
    function van() {
        \$this->$parent_class();
    }
};");

$mystery_machine = new van;
แต่ไม่ค่อยอยากแนะนำให้ใช้ eval เพราะจะเป็นช่วงโหว่ได้ง่าย และ debug ยาก
เขียนแยกเช็คเวอร์ชั่น ดีแล้วครับ

ดูเพิ่มเติม
https://www.php.net/manual/en/language. ... ynamic.php
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 11