วันพุธที่ 23 มีนาคม พ.ศ. 2554

PHP function :: var_export()

var_export

(PHP 4 >= 4.2.0, PHP 5)
var_export — สำหรับแสดง หรือ return ค่าของข้อมูล ของตัวแปร

Description

mixed var_export ( mixed $expression [, bool $return] )
var_export() จะ get ทั้งโครงสร้าง และข้อมูลของตัวแปรที่ส่งเข้าไป ($expression) ซึ่งทำงานเหมือนกันกับvar_dump() ยกเว้นอย่างเดียวคือ ค่าที่ return กลับนั้น เป็น PHP syntax ถูกต้อง (ลองดูตัวอย่างจะเห็นชัดเจนขึ้น)

Parameters 


expression
ตัวแปรที่จะแสดงค่า หรือโครงสร้าง หรือข้อมูล
return
ถ้าเซตค่าเป็น TRUE เพื่อใช้งาน ฟังก์ชั่น var_export() จะ return ข้อมูลของตัวแปร แทนที่จะแสดงออกทาง output เลย
Note: This function internally uses the output buffering with this parameter so it can not be used inside ob_start() callback function.

Return Values

ถ้า parameter return ถูกเซตเป็น TRUE ฟังก์ชั่นนี้จะ return ค่าของข้อมูลของตัวแปรออกไปเป็น string นอกนั้นแล้วจะ return ค่าเป็น NULL

Example

Code
--------------------
<?php
    $myArr = array(
        array('id' => 1, 'name' => 'Mister 01'),
        array('id' => 2, 'name' => 'Mister 02'),
        array('id' => 3, 'name' => 'Mister 03')
    );
   
    echo "output from var_dump():\n";
    var_dump($myArr);
    echo "output from var_export():\n";
    var_export($myArr);
?>
Output--------------------
output from var_dump():
array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(9) "Mister 01"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(9) "Mister 02"
  }
  [2]=>
  array(2) {
    ["id"]=>
    int(3)
    ["name"]=>
    string(9) "Mister 03"
  }
}
output from var_export():
array (
  0 =>
  array (
    'id' => 1,
    'name' => 'Mister 01',
  ),
  1 =>
  array (
    'id' => 2,
    'name' => 'Mister 02',
  ),
  2 =>
  array (
    'id' => 3,
    'name' => 'Mister 03',
  ),
)
จะเห็นว่า output ของ var_export จะมีรูปแบบ เป็น code PHP ที่ถูกต้อง แต่ var_dump ก็จะละเอียดมากกว่า (บอกเลยว่าแต่ละอย่างมี data type เป็นอะไร)

วันจันทร์ที่ 21 มีนาคม พ.ศ. 2554

php OOP เรื่องการใช้ self $this

Scope Resolution Operator ( :: ) ใช้สำหรับเรียก static method ที่ไม่จำเป็นต้องสร้าง object ขึ้นมาก่อน
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden members or methods of a class.
<?php
class A{

function show(){
echo "Hi Narisa";
}

//End Class A

echo A::show(); //<--- เรียกใช้นะครับ
?>
วิธีใช้ ชื่อClass::ชื่อMethod();


====================================
Static Keyword
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.

Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.

Static properties cannot be accessed through the object using the arrow operator ->.

Calling non-static methods statically generates an E_STRICT level warning.

As of PHP 5.2.4, it's possible to reference the class using a variable. 


Example 19.15. Static member example

<?phpclass Foo
{
        public static $my_static = 'foo';

        public function staticValue() {
                return self::$my_static;
        }
}
class Bar extends Foo
{
        public function fooStatic() {
                return parent::$my_static;
        }
}

print Foo::$my_static . "\n";

$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";     // Undefined "Property" my_static 
// $foo::my_static is not possible
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";

$classname = "Bar";
print $classname::$my_static;
?>
แปลคราวๆ น่าจะประมานว่า :: ใช้สำหรับเรียก static method ที่ไม่จำเป็นต้องสร้าง object ขึ้นมาก่อน

operator -> ใช้สำหรับ access ข้อมูลของ object 

mod-rewrite Configuration Server

Configuration Server 
1. ไปที่ start => All programe => AppServ => Configuration Server = > Apache Edit the httpd.conf Configuration File คลิก จะมี notepad เปิดขึ้นมา ไฟล์ชื่อ httpd.conf















2. กด Ctrl+F เพื่อค้นหา LoadModule rewrite_module modules/mod_rewrite.so 
ด้านหน้า LoadModule rewrite_module modules/mod_rewrite.so มีเครื่องหมาย # ให้เอาเครื่องหมายออก




















3. รีสตาร์ท apache ใน appserver 

เช็คว่า server เปิด module mod_rewrite ?

คลิก http://localhost/phpinfo.php แล้ว serch หาคำว่า mod_rewrite ดังรูป