设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

PHP之道

2014-2-13 14:52| 发布者: 红黑魂| 查看: 8592| 评论: 0|来自: github

摘要: 目前网络上充斥着大量的陈旧信息,让PHP新手误入歧途,传播着错误的实践和糟糕的代码,这必须得到纠正。PHP之道网站的目标就是搜集PHP最佳实践、编码规范和网络上的权威学习指南,给PHP学习者提供一个易于阅读,快速 ...

行为驱动开发

行为驱动开发(BDD)有两种方式:SpecBDD和StoryBDD。SpecBDD关注技术行为或代码,而StoryBDD关注业务、特性和交互,这两种方式都有对应的PHP框架。

采用StoryBDD,开发者编写人类可读的故事来描述应用的行为,然后这些故事可以作为应用的测试用例。PHP中用于StoryBDD编程的框架是Behat,从Ruby 的Cucumber项目演化而来,实现了Gherkin DSL来描述特性行为。

采用SpecBDD,开发者编写规格说明来描述实际代码的行为,与测试一个函数或方法不同,规格描述了一个函数或方法应该具有的行为。PHP中的PHPSpec框 架提供该编程方式的支持,它也是从Ruby的RSpec project演化而来。

BDD链接

  • Behat, the StoryBDD framework for PHP, inspired by Ruby’s Cucumber project;
  • PHPSpec, the SpecBDD framework for PHP, inspired by Ruby’s RSpec project;
  • Codeception is a full-stack testing framework that uses BDD principles.

测试辅助工具

除了测试驱动和行为驱动开发框架,还有大量的通用框架和函数库,可以在各种开发方法下使用。

工具链接


服务器和部署

部署PHP应用到线上Web服务器的方式有很多种。

平台即服务(PaaS)

PaaS提供运行PHP Web应用所需的系统和网络环境,对PHP应用和框架只需要做少量的配置即可。

现在PaaS已经成为部署、托管和扩展各种规模的PHP应用的流行方式,可以在 resources部分查看PHP PaaS “平台即服务”供应商列表。

虚拟或独立主机

如果你愿意或想学习系统管理,那么虚拟或独立主机可以让你完全控制自己的运行环境。

nginx和PHP-FPM

PHP通过内置的FastCGI进程管理器(FPM),可以非常高效地和轻量级的高性能Web服务器nginx进行通信。 nginx比Apache消耗更少的内存,能更好的处理并发请求,这在内存限制较多的虚拟主机环境中尤为重要。

Apache和PHP

PHP和Apache是一个老搭档,历史悠久。Apache有很强的可配置性和大量的扩展模块, 是共享主机中常见的Web服务器,完美支持各种PHP框架和开源应用(如WordPress)。可惜的是,默认情况下,Apache比nginx更耗资源,并发处理能力不强。

Apache有多种方式运行PHP,最常见简单的方式是使用mod_php5的prefork MPM方式, 缺点是它对内存的利用效率不高,如果你不想深入学习服务器的管理,那么这种最简单的方式就是你的最佳选择了。注意,如果你使用mod_php5,最好使用 prefork MPM方式。

如果你想追求高性能和高稳定性,那么也可以为Apache选择与nginx类似的FPM系统worker MPM或 event MPM,它们分别使用mod_fastcgi和mod_fcgid模块。FPM方式可以更高效的利用内存,运行 速度更快,但是配置也相对复杂一些。

共享主机

PHP非常流行,很少有服务器没有安装PHP的,因而有很多共享主机,不过需要注意服务器上的PHP是否是最新稳定 版本。共享主机允许多个开发者把自己的网站部署在上面,这样的好处是费用非常便宜,坏处是你不知道将和哪些 网站共享主机,因此需要仔细考虑机器负载和安全问题。如果项目预算允许的话,避免使用共享主机是上策。

Building and Deploying your Application

If you find yourself doing manual database schema changes or running your tests manually before updating your files (manually), think twice! With every additional manual task needed to deploy a new version of your app, the chances for potentially fatal mistakes increase. Whether you’re dealing with a simple update, a comprehensive build process or even a continuous integration strategy, build automation is your friend.

Among the tasks you might want to automate are:

  • Dependency management
  • Compilation, minification of your assets
  • Running tests
  • Creation of documentation
  • Packaging
  • Deployment

Build Automation Tools

Build tools can be described as a collection of scripts that handle common tasks of software deployment. The build tool is not a part of your software, it acts on your software from ‘outside’.

There are many open source tools available to help you with build automation, some are written in PHP others aren’t. This shouldn’t hold you back from using them, if they’re better suited for the specific job. Here are a few examples:

Phing is the easiest way to get started with automated deployment in the PHP world. With Phing you can control your packaging, deployment or testing process from within a simple XML build file. Phing (which is based on Apache Ant) provides a rich set of tasks usually needed to install or update a web app and can be extended with additional custom tasks, written in PHP.

Capistrano is a system for intermediate-to-advanced programmers to execute commands in a structured, repeatable way on one or more remote machines. It is pre-configured for deploying Ruby on Rails applications, however people are successfully deploying PHP systems with it. Successful use of Capistrano depends on a working knowledge of Ruby and Rake.

Dave Gardner’s blog post PHP Deployment with Capistrano is a good starting point for PHP developers interested in Capistrano.

Chef is more than a deployment framework, it is a very powerful Ruby based system integration framework that doesn’t just deploy your app but can build your whole server environment or virtual boxes.

Chef resources for PHP developers:

Further reading:

Continuous Integration

Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily — leading to multiple integrations per day. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.

– Martin Fowler

There are different ways to implement continuous integration for PHP. Recently Travis CI has done a great job of making continuous integration a reality even for small projects. Travis CI is a hosted continuous integration service for the open source community. It is integrated with GitHub and offers first class support for many languages including PHP.

Further reading:


缓存

PHP自身效率很高,但是执行创建远程连接、加载文件等操作时容易出现瓶颈,幸运的是,我们有很多工具来加速这部分操作,或减少 这些耗时操作的执行次数。

字节码缓存

W在一个PHP文件被执行时,它先被编译为字节码(也称opcode),然后这些字节码被执行。如果文件没有修改,那么字节码也会保持不变, 这意味着编译这一步白白浪费了CPU资源。

这就是引入字节码缓存的原因,通过把字节码保存在内存中来消除冗余的编译,重用它们完成后续的调用。配置字节码缓存非常简单, 而且可以极大地提高应用的执行效率,没有理由不使用字节码缓存。

流行的字节码缓存方案有:

对象缓存

很多时候,在代码中缓存对象可以带来很大的收益,例如获取代价很大的数据和查询结果很少变化的数据库调用。我们可以使用对象 缓存系统缓存这些数据,大大加快后续的同类访问请求。如果你在取得这些数据之后,把它们缓存在系统中,在后续对这些数据的请求 中,就可以直接使用缓存中的对象,这么做可以很大的提示系统性能,减少服务器的负载。

很多流行的字节码缓存方案也允许你缓存自定义数据,因此我们更应该充分利用对象缓存功能。APC、XCache和WinCache都提供API, 让你把数据缓存在他们的内存cache中。

使用最多的内存对象缓存系统是APC和memcached,APC是很好的一个对象缓存方案,它提供了简单的API来让你把对象存储在内存中,而且 配置和使用都非常容易,它的一个缺点是只能在本机使用。Memcached则是另外一种方式,它是一个单独的服务,可以通过网络访问,这 意味着可以在一个地方写入数据,然后在不同的系统中访问这份数据。

在单机性能上,APC通常比Memcached更高,如果你不需要多台服务器或者其他Memcached的高级功能,APC可能是你的最佳选择。

APC的示例:


// check if there is data saved as 'expensive_data' in cache
$data = apc_fetch('expensive_data');
if ($data === false) {
    // data is not in cache; save result of expensive call for later use
    apc_add('expensive_data', $data = get_expensive_data());
}

print_r($data);

学习更多对象缓存系统:


资源

From the Source

People to Follow

Mentoring

PHP PaaS供应商

框架

大量的PHP开发者使用框架,而不是重复发明轮子来创建自己的Web应用。框架抽象出底层通用的业务逻辑,给使用者了提供简单易用的接口。

不是所有的项目都需要框架,有时候原生的PHP就能满足需求,但是需要框架的时候,有三种类型的框架可供选择:

  • 微框架
  • 全能(Full-Stack)框架
  • 组件框架

微框架仅是一个包装器(Wrapper),尽量快地把HTTP请求路由到回调函数、控制器或方法上,有些框架也会提供一些函数库,如基本的数据库 操作。微框架主要用于构建远程HTTP服务。

全能框架则是在微框架的功能之上提供了更多的功能特性,如ORM,验证组件等。

组件框架则是一组独立功能库的集合,多个基于组件的框架集合在一起,甚至可以用作微框架或者全能框架。

组件

如前所述,组件是另外一种创建、实现和发布开源代码的方式,当前社区存在很多组件库,最主要的两个:

这两个库都有用于安装和升级的命令行工具,已经在依赖管理部分讲述.

还有基于组件的框架,你可以使用其中的组件,它们相互之间依赖很少,或完全独立,如FuelPHP验证包, 就可以脱离FuelPHP框架而独立使用。这些项目就相当于一个可重用的组件库:


社区

The PHP community is as diverse as it is large, and its members are ready and willing to support new PHP programmers. Consider joining your local PHP user group (PUG) or attending larger PHP conferences to learn more about the best practices shown here. You can hang out on IRC in the #phpc channel on irc.freenode.com and follow the@phpc twitter account. Get out there, meet new developers, learn new topics, and above all, make new friends! Other community resources include the Google+ PHPProgrammer community and StackOverflow.

Read the Official PHP Events Calendar

PHP User Groups

If you live in a larger city, odds are there’s a PHP user group nearby. Although there’s not yet an official list of PUGs, you can easily find your local PUG by searching onGoogle or Meetup.com. If you live in a smaller town, there may not be a local PUG; if that’s the case, start one!

Read about User Groups on the PHP Wiki

PHP Conferences

The PHP community also hosts larger regional and national conferences in many countries around the world. Well-known members of the PHP community usually speak at these larger events, so it’s a great opportunity to learn directly from industry leaders.

Find a PHP Conference



酷毙

雷人
1

鲜花

鸡蛋

漂亮

刚表态过的朋友 (1 人)

  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部