PHP autoload»úÖÆÏê½â
(1) autoload»úÖƸÅÊö
ÔÚʹÓÃPHPµÄOOģʽ¿ª·¢ÏµÍ³Ê±£¬Í¨³£´ó¼ÒÏ°¹ßÉϽ«Ã¿¸öÀàµÄʵÏÖ¶¼´æ·ÅÔÚÒ»¸öµ¥¶ÀµÄÎļþÀÕâÑù»áºÜÈÝÒ×ʵÏÖ¶ÔÀà½øÐи´Óã¬Í¬Ê±½«À´Î¬»¤Ê±Ò²ºÜ±ãÀû¡£ÕâÒ²ÊÇOOÉè¼ÆµÄ»ù±¾Ë¼ÏëÖ®Ò»¡£ÔÚPHP5֮ǰ£¬Èç¹ûÐèҪʹÓÃÒ»¸öÀֻ࣬ÐèÒªÖ±½ÓʹÓÃinclude/require½«Æä°üº¬½øÀ´¼´¿É¡£ÏÂÃæÊÇÒ»¸öʵ¼ÊµÄÀý×Ó£º
CODE:
/* Person.class.php */
<?php
class Person {
var $name, $age;
function __construct ($name, $age)
{
$this->name = $name;
$this->age = $age;
}
}
?>
CODE:
/* no_autoload.php */
<?php
require_once ("Person.class.php");
$person = new Person("Altair", 6);
var_dump ($person);
?>ÔÚÕâ¸öÀý×ÓÖУ¬no-autoload.phpÎļþÐèҪʹÓÃPersonÀ࣬ËüʹÓÃÁËrequire_once½«Æä°üº¬£¬È»ºó¾Í¿ÉÒÔÖ±½ÓʹÓÃPersonÀàÀ´ÊµÀý»¯Ò»¸ö¶ÔÏó¡£
µ«Ëæ×ÅÏîÄ¿¹æÄ£µÄ²»¶ÏÀ©´ó£¬Ê¹ÓÃÕâÖÖ·½Ê½»á´øÀ´Ò»Ð©Òþº¬µÄÎÊÌ⣺Èç¹ûÒ»¸öPHPÎļþÐèҪʹÓúܶàÆäËüÀ࣬ÄÇô¾ÍÐèÒªºÜ¶àµÄrequire/include Óï¾ä£¬ÕâÑùÓпÉÄÜ»áÔì³ÉÒÅ©»òÕß°üº¬½ø²»±ØÒªµÄÀàÎļþ¡£Èç¹û´óÁ¿µÄÎļþ¶¼ÐèҪʹÓÃÆäËüµÄÀ࣬ÄÇôҪ±£Ö¤Ã¿¸öÎļþ¶¼°üº¬ÕýÈ·µÄÀàÎļþ¿Ï¶¨ÊÇÒ»¸öجÃΡ£
PHP5ΪÕâ¸öÎÊÌâÌṩÁËÒ»¸ö½â¾ö·½°¸£¬Õâ¾ÍÊÇÀàµÄ×Ô¶¯×°ÔØ(autoload)»úÖÆ¡£autoload»úÖÆ¿ÉÒÔʹµÃPHP³ÌÐòÓпÉÄÜÔÚʹÓÃÀàʱ²Å×Ô¶¯°üº¬ÀàÎļþ£¬¶ø²»ÊÇÒ»¿ªÊ¼¾Í½«ËùÓеÄÀàÎļþinclude½øÀ´£¬ÕâÖÖ»úÖÆÒ²³ÆΪlazy loading¡£
ÏÂÃæÊÇʹÓÃautoload»úÖƼÓÔØPersonÀàµÄÀý×Ó£º
CODE:
/* autoload.php */
<?php
function __autoload($classname) {
require_once ($classname . "class.php");
}
$person = new Person("Altair", 6);
var_dump ($person);
?>ͨ³£PHP5ÔÚʹÓÃÒ»¸öÀàʱ£¬Èç¹û·¢ÏÖÕâ¸öÀàûÓмÓÔØ£¬¾Í»á×Ô¶¯ÔËÐÐ__autoload()º¯Êý£¬ÔÚÕâ¸öº¯ÊýÖÐÎÒÃÇ¿ÉÒÔ¼ÓÔØÐèҪʹÓõÄÀà¡£ÔÚÎÒÃÇÕâ¸ö¼òµ¥µÄÀý×ÓÖУ¬ÎÒÃÇÖ±½Ó½«ÀàÃû¼ÓÉÏÀ©Õ¹Ãû".class.php"¹¹³ÉÁËÀàÎļþÃû£¬È»ºóʹÓÃrequire_once½«Æä¼ÓÔØ¡£´ÓÕâ¸öÀý×ÓÖУ¬ÎÒÃÇ¿ÉÒÔ¿´³öautoloadÖÁÉÙÒª×öÈý¼þÊÂÇ飬µÚÒ»¼þÊÂÊǸù¾ÝÀàÃûÈ·¶¨ÀàÎļþÃû£¬µÚ¶þ¼þÊÂÊÇÈ·¶¨ÀàÎļþËùÔڵĴÅÅÌ·¾¶(ÔÚÎÒÃǵÄÀý×ÓÊÇ×î¼òµ¥µÄÇé¿ö£¬ÀàÓëµ÷ÓÃËüÃǵÄPHP³ÌÐòÎļþÔÚͬһ¸öÎļþ¼ÐÏÂ)£¬µÚÈý¼þÊÂÊǽ«Àà´Ó´ÅÅÌÎļþÖмÓÔص½ÏµÍ³ÖС£µÚÈý²½×î¼òµ¥£¬Ö»ÐèҪʹÓà in
Ïà¹ØÎĵµ£º
Warning
: Illegal offset type in
Warning
: Illegal offset type in isset or
empty in
Ç°¼¸Ììд³ÌÐòµÄʱºòÅöµ½Ò»¸öÕâÖÖ´íÎóÌáʾ
Èç¹ûÄãʹÓÃÕâÑùµÄ±íʾ·½·¨ÈçÏ£º
$arr = array();
class a
{
}
$o = new a;
echo $arr[$o];
¾Í»á³öÏÖÉÏÃæµÄ´íÎóÌáʾ£¬ÒòΪ²»ÄÜʹÓÃʵÀý»¯µÄ¶ÔÏóÀ´×÷ΪÊý×éµÄË÷Òý£¬»òÕßÔÚʹÓÃi ......
±àÂëÊǸöºÜ»ù´¡µÄÎÊÌ⣬ҲÊÇ´ó¼ÒºÜÈÝÒ׺öÂÔµÄÎÊÌ⣬д´úÂë֮ǰ¶à¶à¿¼ÂÇ£¬ÒÔºó»áÉٺܶàÂé·³¡£
PHP·¢Õ¹Á˲»ÉÙ£¬ÏÖÔÚpearÓÃÆðÀ´ºÜ·½±ã£¬ÆäÖоÍÓÐÏà¹ØµÄclassÀ´¶ÁÈ¡ExcelÎļþÀïÃæµÄÄÚÈÝ£¬Èç¹û²»ÏëʹÓÃpearµÄ»°£¬¿ÉÒÔ¿¼ÂÇʹÓÃexcel_class.php£¬googleһϣ¬¿ÉÒÔÕÒµ½Õâ¸öclassµÄsource codeÏÂÔØ£¬Ò²ÄÜÕÒµ½»ù±¾µÄexample c ......
1¡¢ÔÚÅäÖÃmysqlʵÀýµÄÓïÑÔÑ¡Ïîʱ£¬Ñ¡Ôñutf-8
2¡¢ÔÚhtml,phpÒ³ÃæÖÐ ¼ÓÉÏ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />£¬Èç¹û²»ÐÐÔÙÔÚ phpÎļþ Æðʼ´¦¼Ó header("Content-Type:text/html;charset=utf-8");
3¡¢ÔÚphpÖÐ×öÊý¾Ý¿âÁ¬½Óʱ£¬ÎÒÓõÄʱpdoµÄÐÎʽ£¬Ë ......
(4)Ó³ÉäÀà(ReflectionClass)
ReflectionClassÀàÔÊÐíÄã·´ÏòÓ³ÉäÀà¡£
<?
php
interface MySerializable
{
// ...}
class My
Object
{
// ...}
/** A counter class */
class
Counter
exten ......