 |
Codeigniter ติดปัญหา ใช้ตัวแปร $this เพื่อเรียกใช้ตัวแปรในคลาสไม่ได้ครับ |
|
 |
|
|
 |
 |
|
ผมมี 2 model ทำงานร่วมกันดังนี้
1. class Model_movie extends My_Model
------ function getList($fieldDevice = false, $fieldTable, $ignoreActive = false)
2. class Shall
------ static function getList($fieldDevice = null, $fieldRefTable = null, $ignoreActive = false)
ทั้ง 2 คลาสอยู่ในโฟลเดอร์เดียวกันคือ models และคลาส shall เซ็ตให้ autoload
model_movie.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_movie extends My_Model
{
public $table_name = 'tb_content_movie';
public $primary_key= 'id';
public function __construct()
{
parent::__construct();
$this->load->helper('uploadimage');
$this->config->load('path_upload', TRUE);
}
function getList($fieldDevice = false, $fieldTable, $ignoreActive = false)
{
shall::getList('iphone', 'tb_content_movie', true);
echo shall->$refobjtb;
}
}
?>
shall.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shall
{
public $table_name = '';
public $refobjtb = '';
private static $ci;
function __construct()
{
self::$ci =& get_instance();
}
/**
* [getList list title each shall]
* @param string $fieldDevice [device name for where scope]
* @param string $fieldRefTable [reference table name for where scope]
* @param boolean $ignoreActive [default = false is get device active, true is get device active and inactive]
* @return array object [description]
*/
static function getList($fieldDevice = null, $fieldRefTable = null, $ignoreActive = false)
{
self::setRefobjtb($fieldRefTable);
$sql = '......................'
return self::$ci->db->query($sql)->result();
}
protected function setRefobjtb($tbName = '')
{
$this->$refobjtb = $tbName;
}
}
?>
รันแล้วเกิดเออเร่อตามนี้ครับ
Using $this when not in object context.......
แต่ผมลองเปลี่ยนตัวแปร $refobjtb ให้เป็น static ตอนเซ็ตค่า หรือเรียกใช้ ก็ได้ปรกติ
แก้ยังไงดีครับ ช่วยแนะนำที
ขอบคุณครับ
Tag : PHP
|
ประวัติการแก้ไข 2013-10-10 17:47:13 2013-10-10 17:47:27
|
 |
 |
 |
 |
Date :
2013-10-10 17:45:17 |
By :
fogza |
View :
2738 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เพราะคุณประกาศฟังก์ชั่น setRefobjtb() นี้เป็น instance method
แต่ตอนเข้าถึงคุณใช้ self ซึ่งเป็นการเรียกจาก static context
Code (PHP)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shall
{
public $table_name = '';
public $refobjtb = '';
private static $ci;
function __construct()
{
self::$ci =& get_instance();
}
/**
* [getList list title each shall]
* @param string $fieldDevice [device name for where scope]
* @param string $fieldRefTable [reference table name for where scope]
* @param boolean $ignoreActive [default = false is get device active, true is get device active and inactive]
* @return array object [description]
*/
static function getList($fieldDevice = null, $fieldRefTable = null, $ignoreActive = false)
{
// แต่คุณเรียกมันในแบบ static method
self::setRefobjtb($fieldRefTable);
$sql = '......................'
return self::$ci->db->query($sql)->result();
}
// คุณประกาศฟังก์ชั่นนี้เป็น instance method
// และการเข้าถึง property $refobjtb ก็ผิดนะครับ ต้องเป็น $this->refobjtb
protected function setRefobjtb($tbName = '')
{
$this->$refobjtb = $tbName;
}
}
?>
หากอยากให้เรียก setRefobjtb() จาก static context ได้ คุณต้องทำให้ฟังก์ชั่นนี้เป็น static ด้วย
และประกาศตัวแปร $refobjtb ให้เป็น static และเข้าถึงตัวแปรนี้ด้วย self::$refobjtb
Code (PHP)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shall
{
public $table_name = ''; // อันนี้ควรเป็น static ด้วยหรือเปล่า
// public $refobjtb = '';
public static $refobjtb = '';
private static $ci;
function __construct()
{
self::$ci =& get_instance();
}
/**
* [getList list title each shall]
* @param string $fieldDevice [device name for where scope]
* @param string $fieldRefTable [reference table name for where scope]
* @param boolean $ignoreActive [default = false is get device active, true is get device active and inactive]
* @return array object [description]
*/
static function getList($fieldDevice = null, $fieldRefTable = null, $ignoreActive = false)
{
self::setRefobjtb($fieldRefTable);
$sql = '......................'
return self::$ci->db->query($sql)->result();
}
protected static function setRefobjtb($tbName = '')
{
//$this->$refobjtb = $tbName;
self::$refobjtb = $tbName;
}
}
?>
แต่การทำงานมันจะไม่เหมือนกับ instance variable แล้ว
มันจะเป็นตัวแปรของคลาส คือทุก instance จะใช้ตัวแปรเดียวกัน ค่าเ้ดียวกัน
ต้องเข้าใจเรื่องนี้ก่อนนะครับ ไม่งั้นการ design คลาสของคุณจะไม่ต้องตามความต้องการของคุณ
ลองศึกษา OOP เพิ่มเติมครับ
|
ประวัติการแก้ไข 2013-10-10 23:32:13
 |
 |
 |
 |
Date :
2013-10-10 23:29:47 |
By :
phpinfo() |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$this หมายถึง instance ครับ
มันใช้ไม่ได้เพราะคุณเรียกใช้ method setRefobjtb() ในแบบ static ไงล่ะครับ (แต่คุณประกาศมันในแบบ instance)
self::setRefobjtb() // static
$this->setRefobjtb() // instance
|
 |
 |
 |
 |
Date :
2013-10-11 00:07:11 |
By :
phpinfo() |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มีเรื่องสงสัยอีกข้อครับ
ถ้ามีการประกาศ static function
การอ้างถึงตัวแปร ก็ต้องเป็นตัวแปรประเภท static เหมือนกันใช่ไหมครับ?
|
 |
 |
 |
 |
Date :
2013-10-11 00:44:59 |
By :
fogza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ กระจ่างเลย เมื่อครู่ไปไล่อ่านเว็บภาษาปะกิดมา แต่ยังไม่ชัวร์เลยมาถามให้แน่ใจนะครับ 
|
 |
 |
 |
 |
Date :
2013-10-11 01:15:55 |
By :
fogza |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|