手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:namespace

yii 与 namespace

Yii在import方面已经做的很好了,很多人都说Yii无法导入命名空间,事实上官方很早就给出过意见,如这里:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.namespace,当然,这是中文版的

4. Namespace

不要将路径别名和名字空间混淆了,名字空间是指对一些类名的一个逻辑组合,这样它们就可以相互区分开,即使有相同的名字。 而路径别名是用于指向一个类文件或目录。路径别名与名字空间并不冲突。

提示: 由于 5.3.0 版本之前的 PHP 本质上不支持名字空间,你无法创建两个具有相同名字但不同定义的类的实例。 鉴于此,所有的 Yii 框架类都以字母 'C'(意为 'Class') 作前缀,这样它们可以区分于用户定义的类。我们建议前缀 'C' 只保留给 Yii 框架使用,用户定义的类则使用其他的字母作前缀。

5. 使用命名空间的类

使用命名空间的类是指一个在非全局命名空间下声明的类。比如说,类application\components\GoogleMap 在命名空间application\components下的类。使用命名空间需要 PHP 5.3.0 或者以上版本。

从1.1.5开始,可以无需显式引入而使用一个包含命名空间的类。比如说,我们可以创建一个application\components\GoogleMap 的实例而无需去处理引入的路径,这样就增强了Yii的自动导入机制。

若要自动导入使用命名空间的类,命名空间的格式必须和路径别名相似。比如说,类application\components\GoogleMap 所对应的路径必须和别名application.components.GoogleMap一致。

----------
如果仅看中文版,或许你还是看不懂,但你应该看英文版的,下面还有评论,你就知道怎么用了
评论是这个样子的,LOOK,http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace:

Namespaces in a nutshell

Namespaces in php are like directory paths in console (bash, dos etc)
When you use namespace php keyword like this

namespace a\random\namespace;   class Foo {     static function bar(){}    }

is like executing cd a\specific\directory except that the namespace is created if not exists.
Now everything follows is belonging to that namespace. This means that if you want to instantiate, extend or call a static method from eg foo class on another namespace you have to

//The leading backslash '\' here denotes the global namespace that //is the root folder or C:\ counterpart from console environment  $baz= new \a\random\namespace\Foo;  class Fighter extends \a\random\namespace\Foo {} \a\random\namespace\Foo::bar();

Yii imports a very intuitive convention here that the namespace structure (if implemented) should be reflected on the physical directory structure and additionally makes its Path Alias convenience available for that purpose.
Please be my guest to follow these steps:
1. Create a new web app 2. Go to protected\components and create a folder foo 3. Move Controller.php in foo folder and open it with an editor 4. At line 6, at Controller class declaration import this:

namespace application\components\foo; class Controller extends \CController //Note the leading backslash here
  1. Now open protected\controllers\SiteController.php for editing
  2. Replace the SiteController class declaration with this
class SiteController extends \application\components\foo\Controller

As you will see, your new web app still working fine and application path alias will point properly at protected folder.
You can find more about php namespaces here
Enjoy coding :>

--------
果然,这年头,英文版的资料比中文版多多了,手册上也是,不过PHP手册是一定要带评论,对于我们这些初学者来说,那是相当的有用啊

Tags: yii, namespace

Oauth二三事

这两天在做微博同步,于是对于oauth又重新开始折腾了。其实在之前我也做过类似的笔记,例如这个 关于oauth的几篇文章 ,于是在做这个同步的时候也参考了这些其中的一两篇,比如老王的基于PECL OAuth打造微博应用,但由于我不想加载pecl的oauth库,所以就不能使用了。
于是我就根据官方的SDK进行了处理,但由于需要同时支持两个或更多微博,结果就造成了oauth类的冲突了。而事实上经过测试,还不能加载一个屏蔽一个,因为有些微博对oauth类中的部分做了修改。于是最后我采用了namespace来解决了这个问题。
因为这次我是把weibo的类库做了独立处理,但由于不是每一台服务器都支持5.3的namespace,也在犹豫是不是要重新修正oauth类,但这样之后,官方的SDK我就不能覆盖性的更新了。真纠结呀。
hightman也在重写类似的东西,大家都在重复造轮子,可是怎么办呢?官方万一更新SDK,添加了新接口,我们怎么办,虽然会有一些接口的URL提供我们调用,但总归是直接使用会更方便啦。随便说说而已,目前已经解决,实在吐血的情况下那我就是干脆自己学着新浪他们一样也提供接口。哈哈,做点无耻的事情。。。

Tags: oauth, namespace, weibo, pecl

php simplexmlElement 操作xml的命名空间

这是今天中午发生的事情,有人在群里求助,比如xml中如果标记是<xx:xxxx>content</xx:xxxx>这样的情况下,取不到 xx:xxxx 为下标的值。
看了这个问题,第一个反应就是namespace的关系,但我从来没有使用simplexml操作过namespace,于是就翻开手册查了一下资料,问题并没有解决,最终是通过google解决了该问题。

提问题的朋友贴出了数据源,来自于:http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query,数据结构大致如下:

XML/HTML代码
  1. <feed xmlns='http://www.w3.org/2005/Atom'      xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'      xmlns:gContact='http://schemas.google.com/contact/2008'      xmlns:batch='http://schemas.google.com/gdata/batch'      xmlns:gd='http://schemas.google.com/g/2005'      gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'>      
  2.     <id>liz@gmail.com</id>      
  3.     <updated>2008-12-10T10:04:15.446Z</updated>      
  4.     <category scheme='http://schemas.google.com/g/2005#kind'      term='http://schemas.google.com/contact/2008#contact' />      
  5.     <title>Elizabeth Bennet's Contacts</title>      
  6.     <link rel='http://schemas.google.com/g/2005#feed'      type='application/atom+xml'      href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' />      
  7.     <link rel='http://schemas.google.com/g/2005#post'      type='application/atom+xml'      href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' />      
  8.     <link rel='http://schemas.google.com/g/2005#batch'      type='application/atom+xml'      href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/batch' />      
  9.     <link rel='self' type='application/atom+xml'      href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full?max-results=25' />      
  10.     <author>        
  11.         <name>Elizabeth Bennet</name>        
  12.         <email>liz@gmail.com</email>      
  13.     </author>      
  14.     <generator version='1.0' uri='http://www.google.com/m8/feeds'>      Contacts    </generator>      
  15.     <openSearch:totalResults>1</openSearch:totalResults>      
  16.     <openSearch:startIndex>1</openSearch:startIndex>      
  17.     <openSearch:itemsPerPage>25</openSearch:itemsPerPage>      
  18.     <entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'>        
  19.         <id>        http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de      </id>        
  20.         <updated>2008-12-10T04:45:03.331Z</updated>        
  21.         <app:edited xmlns:app='http://www.w3.org/2007/app'>2008-12-10T04:45:03.331Z</app:edited>        
  22.         <category scheme='http://schemas.google.com/g/2005#kind'        term='http://schemas.google.com/contact/2008#contact' />        
  23.         <title>Fitzwilliam Darcy</title>        
  24.         <gd:name>          
  25.             <gd:fullName>Fitzwilliam Darcy</gd:fullName>        
  26.         </gd:name>        
  27.         <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'        href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de'        gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' />        
  28.         <link rel='self' type='application/atom+xml'        href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' />        
  29.         <link rel='edit' type='application/atom+xml'        href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' />        
  30.         <gd:phoneNumber rel='http://schemas.google.com/g/2005#home'        primary='true'>        456      </gd:phoneNumber>        
  31.         <gd:extendedProperty name='pet' value='hamster' />        
  32.         <gContact:groupMembershipInfo deleted='false'        href='http://www.google.com/m8/feeds/groups/liz%40gmail.com/base/270f' />      
  33.     </entry>    
  34. </feed>  

这个结构在上面的地址里有,这个是我格式化过的XML数据,现在要取得类似于“<gd:phoneNumber rel='http://schemas.google.com/g/2005#home'        primary='true'>        456      </gd:phoneNumber> ”中的值。

最终代码如下:

PHP代码
  1. $x = new SimpleXmlElement($str);  
  2. foreach($x->entry as $t){  
  3.     echo $t->id . "<br >";  
  4.     echo $t->updated . "<br />";  
  5.     $namespaces = $t->getNameSpaces(true);  
  6.     $gd = $t->children($namespaces['gd']);   
  7.     echo $gd->phoneNumber;  
  8. }  

当然,如果不象上面这样写,也可以写成这样:

PHP代码
  1. $x = new SimpleXmlElement($str);  
  2. foreach($x->entry as $t){  
  3.     echo $t->id . "<br >";  
  4.     echo $t->updated . "<br />";  
  5.     //$namespaces = $t->getNameSpaces(true);  
  6.     //注意这里与上面一段的区别  
  7.     $gd = $t->children('http://schemas.google.com/g/2005');   
  8.     echo $gd->phoneNumber;  
  9. }  

只是象第二种写法就属于硬编码了,这样不太好,万一哪天有变化,还得再更改N多代码。
问题接踵而来,比如象下面这段:

XML/HTML代码
  1. <event:event>   
  2. <event:sessionKey></event:sessionKey>   
  3. <event:sessionName>Learn QB in Minutes</event:sessionName>   
  4. <event:sessionType>9</event:sessionType>   
  5. <event:hostWebExID></event:hostWebExID>   
  6. <event:startDate>02/12/2009</event:startDate>   
  7. <event:endDate>02/12/2009</event:endDate>   
  8. <event:timeZoneID>11</event:timeZoneID>   
  9. <event:duration>30</event:duration>   
  10. <event:description></event:description>   
  11. <event:status>NOT_INPROGRESS</event:status>   
  12. <event:panelists></event:panelists>   
  13. <event:listStatus>PUBLIC</event:listStatus>   
  14. </event:event>  

这种非标准的XML,没有定义命名空间,怎么办?在这种情况下,其实SimpleXmlElement就已经直接可以解决了,但是会报warnging,因为他认为event这个命名空间不存在。
解决方法是:

PHP代码
  1. $xml = @new SimpleXmlElement($str);//在前面加@抑止错误。  
  2. echo "<pre>";  
  3. print_r($xml);  

目前看来,这种解决方法比较好。

Tags: php, simplexml, xml, namespace

杂烩,附招聘信息

项目中用到了memcached,类文件是不知道谁找来的MemcacheWrapper,其中有一个方法是namespace(),在正式机上跑的没问题,但是测试机上却老是显示语法有误,说是T_STRING没有正确结尾。开始的时候以为是文件编码导致,但仔细一看,原来是这个namespace名称,但为什么正式机上没问题呢?翻开手册才知道,这个名词是从5.3才开始变成关键字的。这让我想起了一些其他的名称,比如use,goto,import等 。自从命名空间开始后,很多人都认为import会变成关键字,但结果还是采用了use作为关键字。

因此,在写代码的时候,可以看一下PHP附录的保留关键字等。

-------------------------------

一号店,是11在群里说的,说是这个网店吸引了风投,但我看了看,没有什么特别的地方,连京东都可以卖非电器类产品了,这类网站那怎么赢利呢?事实上华联等都有网站,而且送货费也不贵,所以,随便看看喽。。观望一下

-------------------------------

最后,给自己现在的单位进行一下招聘。

条件我也不多说了,总之是PHP方面的人才,一些附加条件我也就多说了,什么精通这个精通那个的。

只有几个条件:1年PHP开发经验的2名,2年+开发经验的2名。1年左右经验的,最基本的CRUD要会,顺便熟悉康盛的系统为佳。2年+的,需要有独立做项目的经验,能够根据需求独立完成项目,当然也不希望是算盘珠一样,一定要有自己的想法才行。

然后就是大家都要求的,性格开朗,有团队经验犹佳之类。那句能够承受加班压力的我就不写了,项目一般不会要求加班,只希望在工作时间把任务完成,这是最佳的。要加班的话,也就是上线前几天和上线后几天,可能会有加班情况出现,但这个,其实大家都懂的吧。

请身在上海,并有意愿从事PHP和已经从事PHP但想換換岗位的可以联系我。可以留言,也可以QQ我:19129540.

Tags: namespace, 一号店, 招聘

PHP的命名空间真这么糟糕么?

PHP的命名空间啊。被很多人所诟病,理由就是那个反斜杠。被当成转义符的反斜杠承担起了命名空间的重任。可想而知,他承受的压力有多大。。。

还好,还是有人在支持的,以下就是一篇支持文章,翻译过的。翻译人当然不是我,我要会翻译也就不写程序了。去翻译点文章忽悠人了。

» 阅读全文

Tags: namespace, 命名空间

Records:612