ผมสังเกตุเห็น Framework & CMS หลายตัวใช้ namespace และมีการตั้งชื่อเล่นให้กับ class ต่างๆ ด้วย class_alias เพื่อให้เรียกใช้ได้ง่ายๆ เช่น ในไฟล์ view คงไม่มีใครอยากใช้

System\Html\Html::image('pic.jpg');

ซึ่ง framework ก็ตั้งชื่อ alias เหลือแค่ Html::image();

คำถามคือ แล้วเราจะใช้ namespace ไปทำไม เพราะสุดท้ายก็ตั้งชื่อเล่นให้กลับไปเหมือนตอนยังไม่มี namespace ให้ใช้ แล้วก็มีปัญหาเรื่องชื่อ class ซ้ำกันอยู่ดี

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

crucifier Mon, 16/03/2015 - 10:06

ผมถามงงๆ ได้คำตอบแล้วตอนนี้ก็ยังงงๆ ที่ผ่านมาได้แต่ใช้ของชาวบ้าน ถึงเวลาต้องมาสร้าง library เองแล้วสับสน T_T

เพราะชื่อ class ใน php มีโอกาศซ้ำกันได้ครับเลยต้องกำหนด namespace เพื่อบอกว่า class นี้อยู่ที่ไหน

ผมเข้าใจ concept ของ namespace ครับ แต่อย่างที่พยายามอธิบายอย่างงงๆ ว่า ทำไมต้องมาตั้งชื่อเล่นให้ Namesapce\Class อีก โดยใช้ฟังก์ชัน class_alias ร่วมกับการทำ overloading เช่น เปลี่ยนการอ้างชื่อยาวๆ จาก

Mvc\Helper\String::replace();

หรือ

use Mvc\Helper\String as String;

ให้สามารถเรียกใช้ได้สั้นๆ แบบนี้โดยตรง

String::replace();

โดยไม่ต้องใช้ keyword use ในทุกไฟล์ที่เรากำลังทำงานอยู่

ยกตัวอย่างที่ใช้วิธีข้างบนก็เช่น Joomla เป็นต้น เข้าใจว่าเพื่อให้เรียกใช้ได้ง่ายจาก component แต่ผมสงสัยว่าอุตส่าห์ใช้ namespace แล้ว ยังจะมาตั้งชื่อเล่น (alias) ให้มันอีก สุดท้ายหากมีคลาสชื่อซ้ำกันในอนาคตก็พังอยู่ดี

แต่ Drupal 7 ปรากฎว่าใช้ keyword use ในทุกไฟล์ เช่นไฟล์ template นี่ อ้าง keyword use กันเป็น 10 บรรทัด

จึงสงสัยว่ามันมีเหตุผลอะไรที่ Joomla ต้องทำอย่างนั้น?

--

อันนี้ไม่เกี่ยวกับประเด็นข้างต้น แต่เป็นความคิดเห็นว่าการต้องมาใช้ keyword use นี่เสียเวลานะ บางทีทำงานไปเรื่อยๆ บาง class ไม่ได้ถูกเรียกใช้แล้ว แต่ยังประกาศไว้โดย keyword use ไม่ได้เอาออกก็มี

//index.php
use Mvc\Helper\String as String;
use Mvc\Helper\String2 as String;

ถ้าตั่งแบบนี้พังแน่นอน เพราะว่า ชื่อซ้ำกัน

แต่ถ้า

//index.php
use Mvc\Helper\String as String;

//index2.php
use Mvc\Helper\String2 as String;

แบบนี้ไม่น่าจะพัง

ไม่รู้ว่าเวลาใช้งานจะเรียกชื่อ file แบบนี้หรือเปล่า

//index.php
use Mvc\Helper\String as String;
use Mvc\Helper\Chat as Chat;
use Mvc\Helper\Action as Action;
use Mvc\Helper\Controller as Controller;
use Mvc\Helper\Router as Router;
...
...
...

ถ้าทำแบบนี้น่าจะเขียนไม่รัดกุมนะครับ คือเราเอาแค่ class ที่เราจะต้องใช้ในหน้านั้นเท่านั้นก็พออันไหนไม่ได้ใช้ก็ไม่ต้องประกาศ

หมายเหตุ การเรียกใช้น่าจะใช้ประมาณแบบนี้ ถ้าผิดพลาดก็ขอโทษด้วยนะครับเพราะผมไม่ได้เขียน php นานแล้ว

//index.php
use Mvc\Helper\String as String;

//index2.php
use Mvc\Helper\String2 as String;

แบบนี้พังครับ ลองแล้ว ^^ แต่นั่นยังเป็นคนละประเด็นกับที่ผมพูดถึงครับ (เรื่องมันซับซ้อนเนาะ อธิบายยาก 555)

คำว่าชื่อเล่นของผมไม่ได้หมายถึงการใช้ use ... as ครับ แต่หมายถึงการใช้ฟังก์ชัน class_alias ตั้งชื่อเล่นให้มันตั้งแต่ตอนทำ overloading เลย ซึ่งตอนนี้ผมไม่สนใจละว่าทำไม เพราะ...

หลังจากผมนั่งหาข้อมูลและแกะโค้ดชาวบ้าน รวมถึงทดลองทำด้วยตนเองมา 2-3 วัน พอจะจับทาง (เดา) ได้ว่าที่ Joomla! ต้องตั้งชื่อเล่นให้ namespace นอกจากเพื่อให้เรียกใช้งานได้ง่ายๆ แล้ว (ไม่ต้องมา use ทุกครั้งในทุกไฟล์) ก็เพื่อให้ legacy code ทำงานได้ด้วย

ส่วน drupal 8 ตัว stable ยังไม่ release เลยสามารถใช้แนวทางการเขียนแบบใหม่ได้เลย (use กระจาย)