php 桌面应用程序_PHP应用程序环境

php 桌面应用程序

phpenv

The following is a short extract from our recent book, Jump Start PHP Environment, available for free to SitePoint Premium members. Print and ebook copies are sold in stores worldwide, or you can order them here. We hope you enjoy this extract and find it useful.

以下是我们最近的书《 Jump Start PHP Environment》的简短摘录, SitePoint Premium成员可以免费获得。 印刷和电子书的副本已在世界各地的商店出售,或者您可以在此处订购 我们希望您喜欢此摘录并觉得有用。

This piece will focus on the application environment. We’ll also discuss *AMP bundles such as XAMPP and why they’re a poor choice; production /development parity; and performance and debugging.

本文将重点介绍应用程序环境。 我们还将讨论* AMP捆绑包(例如XAMPP)以及为什么它们不是一个好的选择。 生产/发展平价; 性能和调试。

应用环境 (Application Environments)

The application environment is the term used to describe the environment your application can find itself in during various stages of its life cycle: the production environment, the development environment, and the staging environment.

应用程序环境是一个术语,用于描述应用程序在其生命周期的各个阶段(生产环境,开发环境和登台环境)可以找到的环境。

The word environment refers to the hardware and software around your application; that is, everything used to power it.

环境一词是指应用程序周围的硬件和软件。 也就是说,所有用于驱动它的东西。

生产 (Production)

We’ll start with the most straightforward of environments: the production environment.

我们将从最简单的环境开始: 生产环境

When you deploy your application―in other words, upload it to a server and make it publicly accessible for the target audience―you’re putting it in production, or in use. The production environment is your application’s final destination, your code’s purpose.

部署应用程序时(换句话说,将其上传到服务器并使其可供目标受众公开访问)是指将其投入生产使用中 。 生产环境是应用程序的最终目标,也是代码的目的。

In an application’s life cycle, the production environment is the live server―a server computer that’s set up so that other people can connect to it and see your website. This live server will be configured to serve your application as efficiently as possible. All the extra files you used during development will be removed from the application via a process known as compiling or building, which is explained further on.

在应用程序的生命周期中,生产环境是实时服务器 -设置为服务器计算机,以便其他人可以连接到该服务器并查看您的网站。 该实时服务器将配置为尽可能有效地为您的应用程序提供服务。 您在开发过程中使用的所有其他文件都将通过称为编译或构建的过程从应用程序中删除,这将在后面进一步说明。

When in production, your site is considered to be live (or deployed) and will be accessible via its own domain; for example, http://mysite.com. When you launch your site (put it into production mode), you have cause for celebration, because this is the final step in your application’s development process.

在生产中,您的站点被认为是实时的(或已部署),并且可以通过其自己的域进行访问; 例如, http://mysite.com 。 启动网站(使其进入生产模式)时,您很有理由庆祝,因为这是应用程序开发过程中的最后一步。

It is the equivalent of a chef cooking a meal in a restaurant and having it delivered to the patrons who ordered it.

这相当于厨师在饭店里做饭,然后将饭菜交付给点餐的顾客。

发展历程 (Development)

In the development environment, your application is being actively developed. The development environment is the computer you as the developer are using to develop the app, including the computers of all your team members, regardless of whether they are near you or remote. It is important to note that despite covering both aspects, the development environment refers more to the state your app is in, rather than its physical location―the app is in the state of being developed.

在开发环境中,正在积极开发您的应用程序。 开发环境是您作为开发人员用来开发应用程序的计算机,包括您所有团队成员的计算机,无论他们在您附近还是偏远。 需要注意的重要一点是,尽管涵盖了两个方面,但开发环境更多地是指您的应用所处的状态 ,而不是其实际位置,即应用处于开发中的状态。

In the development environment, you have various tools at your disposal―from IDEs (see Chapter 2) to unit testing libraries and standards fixers, compilers and builders, file watchers, and more―anything you need to achieve the job at hand.

在开发环境中,您可以使用各种工具-从IDE(请参阅第2章)到单元测试库和标准修复程序,编译器和生成器,文件监视程序等等,这些都是您手头所需的一切。

If we compare our application to a smartphone, the assembly plant it’s being made in is the development environment. This environment contains all the necessary parts―the screen, the case, the battery, the various LEDs―and each part is individually tested before being used in the construction of a smartphone unit. This is called unit testing―ensuring each unit works.

如果将我们的应用程序与智能手机进行比较,则其制造厂就是开发环境。 该环境包含所有必要的部件-屏幕,外壳,电池,各种LED-并且每个部件在用于智能手机单元的构造之前都经过单独测试。 这称为单元测试-确保每个单元正常工作。

For an example of unit testing in application development, see the section called “For Those Who Want More” at the end of this chapter.

有关应用程序开发中的单元测试的示例,请参见本章末尾的“对于那些想要更多的人”部分。

To take the smartphone assembly analogy further, the battery being tested might require a separate charger attachment, or the screen may need to be tested using a robotic arm with synthetic fingers to ensure that the screen’s touch sensitivity works.

为了进一步模拟智能手机的装配,被测试的电池可能需要单独的充电器附件,或者可能需要使用带有合成手指的机械臂来测试屏幕,以确保屏幕的触摸灵敏度正常。

robot

All these add-ons are there during development only. When converting from the development environment to the production environment (also known as deploying), these add-ons are removed. For our application, this means the aforementioned compilation/building step: various CSS and JavaScript files are merged together and shrunk so as to decrease the size of the website, making it appear faster when people visit it; unit tests are ignored and left behind in the development environment; and various other optimizations occur (covered later in the chapter)―all with the intention of making the final product maximize its appeal and potential when declared ready.

所有这些附加组件仅在开发期间存在。 从开发环境转换为生产环境(也称为deploy )时,将删除这些附加组件。 对于我们的应用程序,这意味着前面提到的编译/构建步骤:将各种CSS和JavaScript文件合并在一起并缩小,以减小网站的大小,从而在人们访问网站时显得更快; 在开发环境中,单元测试将被忽略并留在后面; 并进行了各种其他优化(在本章后面介绍),所有这些优化的目的都是使最终产品在准备就绪时能发挥最大的吸引力和潜力。

主机和虚拟主机 (Hosts and Virtual Hosts)

When you develop on your own computer, it’s impossible to visit the URL http://mysite.com and expect to see your site; after all, your site is yet to go live―it’s not on the Internet. To get around this and see our site as if it were live, we fake the Internet by defining virtual hosts.

当您在自己的计算机上进行开发时,无法访问URL http://mysite.com并希望看到您的网站; 毕竟,您的网站尚未启用-它不在Internet上。 为了解决这个问题,让我们的网站看起来像真实的一样,我们通过定义虚拟主机来伪造Internet。

Put simply, a virtual host gives an instruction to the server program installed on your computer, such as: IF a user requests http://mysite.com in the browser, run the file mysite.php through PHP and show its output in the browser.

简而言之, 虚拟主机向安装在您计算机上的服务器程序发出指令,例如:如果用户在浏览器中请求http://mysite.com ,通过PHP运行文件mysite.php并在浏览器。

As you can see, this is nearly identical to the regular request flow of live sites. But what makes the browser ask our own computer’s server program for the mysite.com domain, rather than look it up on the Internet’s name servers? The hosts file.

如您所见,这几乎与活动站点的常规请求流相同。 但是,是什么使浏览器向我们自己计算机的服务器程序询问mysite.com域,而不是在Internet的名称服务器上查找? hosts文件。

The hosts file is a special file present on every operating system. We mentioned it briefly in the section called “For Those Who Want More” in Chapter 1. It contains a list of domains and their appropriate IP addresses, so that any browser on your computer can read it and go directly to the IP address without having to talk to the DNS to check where to go. On Windows, that file is in C:\Windows\System32\drivers\etc\hosts, and on Linux and Mac machines it’s in /etc/hosts. If you put an IP-name pair into this file, the computer will obey it. We can even try it out right now. Have no fear―there’s nothing that can go wrong. Ready?

hosts文件是每个操作系统上都存在的特殊文件。 我们在第1章的“针对那些想要更多的人”部分中简要提到了它。它包含一个域列表及其适当的IP地址,以便您计算机上的任何浏览器都可以读取它并直接转到IP地址,而无需与DNS交谈以检查要去哪里。 在Windows上,该文件位于C:\Windows\System32\drivers\etc\hosts ,而在Linux和Mac计算机上,该文件位于/etc/hosts 。 如果将IP名称对放入此文件,计算机将服从它。 我们甚至可以立即尝试。 不用担心-没有什么可以出错的。 准备?

On Windows, enter the search field, type in “notepad” and once it appears, right-click it and select Run as Administrator. The system will then ask you for confirmation. In the window that opens, select File > Open and go to: My Computer > C: > Windows > System32 > drivers > etc. In the lower right corner of the Notepad window, you might need to select All Files so that the hosts file shows. Double-click to open it.

在Windows上 ,输入搜索字段,键入“ notepad”,一旦出现,请右键单击它并选择“以管理员身份运行” 。 然后,系统将要求您进行确认。 在打开的窗口中,选择文件>打开,然后转到: 我的电脑> C:> Windows> System32>驱动程序>等等 。 在“记事本”窗口的右下角,您可能需要选择“ 所有文件”,以便显示hosts文件。 双击打开它。

On Linux/Mac machines, open Terminal by searching for it. On Linux, open the default text editor as the administrator by typing sudo gedit into Terminal. You’ll be asked for your administrator password. On OS X, enter sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit, which will do the same task. In either of these editors, go to File -> Open and enter the directory /etc to find the hosts file. Double-click to open it.

在Linux / Mac计算机上,通过搜索打开“终端”。 在Linux上 ,通过在终端中输入sudo gedit以管理员身份打开默认的文本编辑器。 系统将要求您输入管理员密码。 在OS X上 ,输入sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ,它将执行相同的任务。 在这两个编辑器中,转到“ 文件”->“打开”,然后输入目录/etc查找hosts文件。 双击打开它。

Once the file is opened, note the first few lines: they’ll all start with the hash symbol (#). This indicates that they’re comments, and have no effect on the file. These serve to explain a file’s purpose to the user, and they exist in PHP as well.

打开文件后,请注意前几行:它们均以井号(#)开头。 这表明它们是注释 ,对文件没有影响。 这些用于向用户解释文件的用途,它们也存在于PHP中。

Now, under all these comment lines, add the following line:

现在,在所有这些注释行下,添加以下行:

208.117.229.217 bing.com

Save the file and open http://bing.com in your browser. You’ve just successfully redirected all requests to Microsoft’s search engine Bing to Google! Of course, we don’t want to keep these changes; feel free to delete this line or put a hash symbol in front of it to turn it into a comment, and save the file. You should be able to visit http://bing.com as normal again.

保存文件并在浏览器中打开http://bing.com 。 您已经成功地将所有请求重定向到Microsoft的搜索引擎Bing到Google! 当然,我们不想保留这些更改。 您可以随时删除此行,也可以在其前面添加一个井号以将其转换为注释,然后保存文件。 您应该可以再次正常访问http://bing.com

Using this method, we’ll later redirect all browser requests for http://mysite.com (which will be our application’s example domain) to our own computer’s server. This will enable us to easily test the development version of our site without deploying it live.

使用此方法,我们稍后将重定向所有对http://mysite.com (这是我们的应用程序的示例域)的浏览器请求到我们自己的计算机的服务器。 这将使我们能够轻松测试站点的开发版本,而无需实时部署它。

暂存和维护 (Staging and Maintenance)

The staging environment is a separate server (or several servers) containing a copy―also known as a mirror―of the production environment. The staging environment is often skipped in smaller companies or projects. It is designed to resemble the production environment as closely as possible, with matching versions of installed software, identical configuration values, and so on. Staging is for performing the final tests; for example, Facebook might redesign its front page, and before deploying it out into production for all users to see, it would deploy to its staging servers so that employees (dedicated non-programmers also known as the quality assurance team) can test everything first as if using it regularly. If everything goes well, the final deployment from staging to production happens.

登台环境是一个单独的服务器(或多个服务器),其中包含生产环境的副本(也称为镜像)。 在较小的公司或项目中,通常会跳过暂存环境。 它被设计为与生产环境尽可能相似,具有已安装软件的匹配版本,相同的配置值等。 分阶段用于执行最终测试; 例如,Facebook可能会重新设计其首页,并且在将其部署到生产环境中以供所有用户查看之前,它将部署到其登台服务器上,以便员工(专用的非程序员,也称为质量保证团队)可以首先测试所有内容。就像定期使用一样。 如果一切顺利,则将进行从阶段到生产的最终部署。

Staging is beyond the scope of this book, but it’s good to know about it. In the age of virtual machines and small throw-away projects, staging can be unnecessary, only coming into play with bigger long-term web applications.

分期超出了本书的范围,但是很高兴知道这一点。 在虚拟机和小型一次性项目的时代,暂存可能是不必要的,只有在较大的长期Web应用程序中才能发挥作用。

There is also maintenance mode, a term we must cover in this context. It’s a mode rather than an environment because the environment around the application doesn’t change―only the status of the application does. It’s usually just a switch on the production server telling those trying to access the website words to the effect of “Be right back, tuning up!”

这里还有维护模式 ,这是我们必须涵盖的术语。 这是一种模式,而不是环境,因为应用程序周围的环境不会更改-仅应用程序的状态会更改。 通常,这只是生产服务器上的一个开关,告诉那些试图访问该网站的用户,其效果是“马上回来,调优!”

* AMP捆绑包的邪恶 (The Evil of *AMP Bundles)

When starting out in PHP development, it’s tempting to download and install packages such as XAMPP, WAMP, MAMP, or EasyPHP. The AMP in those names stands for “Apache, MySQL, and PHP.” XAMPP adds another P at the end for the Perl language. The first letter refers to the operating system: Windows, Linux, Mac OS X, or, in the case of XAMPP, cross-platform (meaning it works on any OS).

开始进行PHP开发时,很容易下载和安装XAMPP, WAMP, MAMPEasyPHP等软件包。 这些名称中的AMP代表“ Apache,MySQL和PHP”。 XAMPP在Perl语言的末尾添加了另一个P。 第一个字母是指操作系统:Windows,Linux,Mac OS X,或者对于XAMPP,是跨平台的(意味着它可以在任何OS上运行)。

These bundles contain all the software you need to run your PHP applications quickly and easily on your computer. In one click, you can install everything you could ever need to write your very first PHP script. So, where’s the evil in this?

这些捆绑软件包含在计算机上快速轻松地运行PHP应用程序所需的所有软件。 只需单击一下,就可以安装编写第一个PHP脚本所需的一切。 那么,这其中的邪恶在哪里?

  • your computer will be polluted with unnecessary software

    您的计算机将被不必要的软件污染

  • you’ll learn less than you would by installing manually

    您将比手动安装学到的东西少

  • testing is diffficult

    测试很困难

  • if you make a mistake, it’s either very difficult or impossible to return to a previous state

    如果您犯了一个错误,则很难或不可能回到以前的状态

Let’s address these one by one.

让我们一一解决。

机器污染 (Machine Pollution)

Whenever you install software such as an *AMP stack on your computer, a chunk of your computer dies. Even if you delete the software later on, uncomfortable traces usually remain―often in the form of registry entries in Windows or file dust on Linux. In fact, this is especially apparent on Linux. While Windows and OS X applications are installed in an application folder with all related files inside it, on Linux the installation of software is like taking a shotgun to a castle built of Legos. One click and they’re everywhere.

每当您在计算机上安装诸如* AMP堆栈之类的软件时,计算机的大部分就会死掉。 即使稍后删除软件,通常也会留下不舒服的痕迹,通常以Windows中的注册表项或Linux中的文件尘埃的形式出现。 实际上,这在Linux上尤其明显。 Windows和OS X应用程序安装在其中包含所有相关文件的应用程序文件夹中时,在Linux上,软件的安装就像将shot弹枪带到Legos建造的城堡中。 一键点击,它们无处不在

As time goes on, you’ll install another library, another package, another tool. You’ll keep adding supplementary software as you develop your app further, perhaps add different items entirely because you’ve started working on a new project in parallel. Maybe app1 requires a PHP extension to edit images, and app2 needs a PHP extension that allows it to package code into closed source archives, so that your code is hidden from your competition. In time, you’ll have hundreds of megabytes of development software sitting on your machine, with no clue as to whether or not you still need it.

随着时间的流逝,您将安装另一个库,另一个软件包,另一个工具。 在进一步开发应用程序时,您将继续添加辅助软件,也许完全添加了不同的项目,因为您已经开始并行处理新项目。 也许app1需要一个PHP扩展来编辑图像,而app2需要一个PHP扩展来允许将代码打包到封闭源代码存档中,从而使您的代码在竞争中处于隐藏状态。 随着时间的流逝,您的计算机上将拥有数百兆的开发软件,而您是否仍需要它就毫无头绪。

Your machine will slow down, applications will become less usable, and your development machine―the computer you’re working on―will become so different from the production server you eventually intend to deploy your application on that you’ll be unable to handle the difference gracefully. You’ll end up regularly combating bugs on a live website and annoying your visitors to no end.

您的计算机将变慢,应用程序将变得不那么可用,而开发计算机(您正在使用的计算机)将与最终打算在其上部署应用程序的生产服务器完全不同,以致您将无法处理应用程序。优雅地区别。 您最终将定期与实时网站上的错误进行斗争,并让您的访问者无休止地烦恼。

学习就是进步,或者舒适区的舒适度被高估了 (Learning Is Progress, or How the Comfort of Your Comfort Zone Is Overrated)

By depending on these prebuilt packages, you also rob yourself of the experience of learning system administration work (ops, in short). Ops is, in larger companies, a team or person in charge of server issues―whether it be fixing bugs, installation of new software, upgrading existing software, and so on. In smaller teams or when working solo for clients, basic system administration is an essential skill to have.

通过依赖这些预先构建的软件包,您还可以失去学习系统管理工作的经验(简称ops )。 在较大的公司中,Ops是负责服务器问题的团队或负责人-无论是修复错误,安装新软件,升级现有软件,等等。 在较小的团队中或为客户独自工作时,基本系统管理是必不可少的技能。

While being able to install everything you need onto your computer with a single click is neat, there’s no user interface on a server and thus nothing to click; you need to master the commands required to set up a server’s software so that it can run your PHP application. Otherwise, you’re either destined to hire a server administrator to help you out, or even worse, use shared hosting (a horror story that’s explained in Chapter 6).

一次单击即可将所需的一切安装到计算机上的方法很简单,但是服务器上没有用户界面,因此无需单击即可; 您需要掌握设置服务器软件所需的命令,以便它可以运行您PHP应用程序。 否则,您要么注定要雇用服务器管理员来帮助您,要么更糟的是使用共享主机(第6章中介绍了一个恐怖的故事)。

By refusing to rely on these *AMP bundles, you’ll be forced to climb the learning curve of installing a server and other software by hand—knowledge that’ll be useful in more ways than one if you’re serious about this career path. Besides, nailing down the basics is really not that hard, as you’ll see later in the book.

通过拒绝依赖这些* AMP捆绑软件,您将被迫爬上手工安装服务器和其他软件的学习曲线-如果您认真对待这一职业道路,那么与其他人相比,该知识将以更多的方式有用。 此外,扎根基础确实并不难,正如您将在本书后面看到的那样。

测试中 (Testing)

Say app1 and app2 are built on PHP 5.3, run MySQL 4.0, and are intended to go live on a server powered by Apache (the server software). Then, there’s a new requirement: make sure app1 works on PHP 5.6 and MySQL 5.1, and can be powered by Nginx (another server program that’s competition to Apache, pronounced “engine x”). Uh-oh, what now?

假设app1app2建立在PHP 5.3上,运行MySQL 4.0,并且打算在由Apache驱动的服务器(服务器软件)上运行。 然后,有一个新的要求:确保app1可在PHP 5.6和MySQL 5.1上运行,并且可以由Nginx(与Apache竞争的另一种服务器程序,称为“ engine x”)提供支持。 呃,现在怎么办?

We could update PHP to a newer version and check whether app1 still works, but how do we then keep developing app2 without accidentally using code that’s unavailable in PHP 5.3 if our entire computer is now running 5.6? Likewise, we could upgrade MySQL to 5.1 and check that it still works, but how do we know MySQL didn’t throw out some old features in version 5+ that would break app2 even if we fixed app1 to work on 5.1? After all, app2 still needs to work on 4.0 because it’s probably still deployed on such a production server. Heck, how do we handle the Apache versus Nginx problem? Do we install both web servers on our computer and test for each? How do we switch them out? How do we ensure that we remember to test our site on one while the other is running?

我们可以将PHP更新到较新的版本,并检查app1是否仍然有效,但是如果整个计算机现在都在运行5.6,我们如何继续开发app2而不意外使用PHP 5.3中不可用的代码? 同样,我们可以将MySQL升级到5.1并检查它是否仍然有效,但是我们怎么知道MySQL没有抛出版本5+中的一些旧功能,即使我们将app1固定在5.1上,它也会破坏app2? 毕竟,app2仍需要在4.0上运行,因为它可能仍部署在这样的生产服务器上。 哎呀,我们如何处理Apache与Nginx问题? 我们是否将两个Web服务器都安装在我们的计算机上并分别进行测试? 我们如何将它们切换出来? 我们如何确保我们记得在另一个网站上运行时测试我们的网站?

And that’s just for two apps. Now imagine if you had to deal with a dozen applications from ten different clients, each with different requirements. It all stops being fun very, very fast.

这仅适用于两个应用程序。 现在想象一下,如果您必须处理来自十个不同客户端的十几个应用程序,每个都有不同的要求。 这一切都非常非常快地停止了乐趣。

This separation of different software versions is easy to solve by means of virtual machines, which we’ll talk about in Chapter 4.

可以通过虚拟机轻松解决不同软件版本的分离问题,我们将在第4章中讨论。

不可洗的混乱 (The Uncleanable Mess)

Finally, what if we try to install a new version of PHP, but something goes wrong? This scenario is especially common in OS X and Linux―now none of the PHP versions we have installed on the machine work and it’s proving impossible to get a single site to run. What a mess! Instead of easily reverting to a previous running state, we’re forced to spend the entire day debugging our own system and trying to make it run―not necessarily the right version, but just run.

最后,如果我们尝试安装新版本PHP,但出现问题怎么办? 这种情况在OS X和Linux中尤其常见-现在我们在计算机上安装的所有PHP版本都无法正常工作,并且证明无法运行单个站点。 真是一团糟! 我们没有轻松地恢复到先前的运行状态,而是被迫花了一整天时间调试我们自己的系统并试图使其运行-不一定是正确的版本,而只是运行

Wouldn’t it be great if we could just enter a command and have things go back to the way they were 10 minutes ago? Well, we can! This, too, will be demonstrated in Chapter 4.

如果我们只输入一个命令并让事情回到10分钟前的状态,那不是很好吗? 好吧,我们可以! 这也将在第4章中进行演示。

生产/开发平价 (Production/Development Parity)

This complicated-sounding phrase is actually very simple―we’ve actually indirectly touched on it before. Production/development parity means nothing more than having the production and development environment as similar as possible to each other, preferably identical, so that anything you develop will automatically run in production without any excessive configuration or additional debugging or tweaking.

这个听起来听起来很复杂的短语实际上非常简单-我们之前实际上已经间接涉及过它。 生产/开发对等仅意味着使生产和开发环境彼此尽可能相似,最好是完全相同,因此您开发的任何内容都将自动在生产中运行,而无需任何过多的配置或其他调试或调整。

Achieving parity is very important for one’s workflow due to the enormous amount of time it saves. Avoiding having to do any additional work for your application to run in production means having the freedom and time to focus on important business-related logic problems that actually benefit your application’s context, rather than being stuck in a rut of constantly playing catch-up. Making a change in development and then having to make two changes in production for that change to become apparent is tedious at best and detrimental to a project’s health at worst. You never know who on the team will slip up and cause the app to greet people with an error screen.

实现奇偶校验对于一个人的工作流程非常重要,因为它可以节省大量时间。 避免为使应用程序在生产环境中运行而要做任何额外的工作,意味着您有足够的时间和时间专注于与业务相关的重要逻辑问题,这些问题实际上对您的应用程序上下文有利,而不必陷入不断追赶的麻烦。 进行开发更改,然后必须在生产中进行两次更改才能使更改变得明显,这既乏味,又不利于项目的健康。 您永远不会知道团队中的谁会滑倒并导致应用程序以错误屏幕与人们打招呼。

The best way to achieve parity is by having the exact same software running in your development environment as you do in your production environment. For example, if you’re aiming to deploy your application to production onto a server running the Ubuntu Linux version 14.04 operating system, it’s best that you develop on this operating system as well. However, what if we were running Windows, because we like to consume advanced multimedia content such as games, or we need powerful image and video manipulation software that just cannot exist on a Linux OS? Should we forsake all our other interests, install Linux over Windows, and strive for parity over anything else? Or should we just forsake parity and risk it while keeping our computer powerful, beautiful, and stable, sticking to our operating system of choice?

实现奇偶校验的最佳方法是在开发环境中运行与在生产环境中运行的软件完全相同的软件。 例如,如果您打算将应用程序生产部署到运行Ubuntu Linux 14.04操作系统的服务器上,则最好也使用该操作系统进行开发。 但是,如果我们运行Windows,是因为我们喜欢消费游戏等高级多媒体内容,还是需要功能强大的图像和视频处理软件,而这些软件在Linux OS上是不存在的? 我们是否应该放弃所有其他利益,在Windows上安装Linux,并在其他方面寻求平价? 还是在保持计算机功能强大,美观,稳定并坚持使用我们选择的操作系统的同时放弃同等的风险?

Fortunately, there’s a third way that allows you to achieve the best of both worlds: virtual machines (covered in Chapter 4).

幸运的是,还有第三种方法可以让您实现两全其美:虚拟机(在第4章中介绍了)。

性能与调试 (Performance and Debugging)

The last aspects of the application environment we need to touch on is ensuring that the app is fast (performance) and bug-free (debugging). This section is entirely theoretical; it’s just so you know what to expect later on when we come across some of these terms.

我们需要涉及的应用程序环境的最后方面是确保应用程序快速(性能)和无错误(调试)。 本节完全是理论上的; 只是为了让您知道以后遇到这些术语时会期待什么。

Performance is achieved through various methods of optimization. Contrary to the word’s meaning, in application development optimization often has several layers and is rarely the change that immediately brings about the perfect solution. Performance includes, but is not limited to, several areas:

通过各种优化方法可以实现性能。 与这个词的含义相反,在应用程序开发中,优化通常具有多个层次,而很少是立即带来完美解决方案的更改。 性能包括但不限于以下几个方面:

Optimizing the database 优化数据库

Often the slowest part of any website, the database can benefit from additional optimizations after it’s been running for a while and the bottlenecks (the parts that are the slowest because they’re unable to process the number of incoming requests fast enough) become apparent. Methods for database optimization include setting up indexes, splitting reads and writes, changing database engines, caching fetched data, and other mystical-sounding phrases.

数据库通常是任何网站中最慢的部分,运行了一段时间后,可以从其他优化中受益,并且瓶颈(最慢的部分,因为它们无法足够快地处理传入请求的数量)变得显而易见。 数据库优化的方法包括设置索引,拆分读取和写入,更改数据库引擎,缓存获取的数据以及其他听起来很神秘的短语。

Optimizing the front-end assets 优化前端资产

We mentioned compilation and building earlier―it’s how we optimize the website’s front end. When a website is shown to users, they see output comprised of images, HTML, CSS, and JavaScript, all of which need to be downloaded and executed in the browser as explained in Chapter 1. The smaller these files are―and the fewer there are―the faster a website loads. Often, a website will have multiple CSS files and multiple JavaScript files. Combining each type into one bigger CSS or JavaScript file yields dramatic increases in a website’s download speed. Another front-end asset optimization trick often done is serving images via a content delivery network, or CDN, a third-party service that hosts your images for you and ensures that the visitor to your website downloads them from a server closest to them, thereby further increasing speed. One can also reduce image size, create an image sprite by placing all images into one file, and more.

我们前面提到过编译和构建,这是我们优化网站前端的方式。 当向用户显示网站时,他们会看到由图像,HTML,CSS和JavaScript组成的输出,所有这些都需要在浏览器中下载并执行,如第1章所述。这些文件越小,其中的文件就越少是-网站加载速度更快。 一个网站通常会具有多个CSS文件和多个JavaScript文件。 将每种类型组合成一个更大CSS或JavaScript文件可大大提高网站的下载速度。 经常进行的另一项前端资产优化技巧是通过内容传递网络 (CDN)来提供图像,CDN是一种第三方服务,可为您托管图像,并确保您网站的访问者从距离它们最近的服务器上下载图像,从而进一步提高速度。 您还可以缩小图像尺寸,通过将所有图像放入一个文件来创建图像精灵 ,等等。

Optimizing the back end 优化后端

This is also a compiling/building step. Test files are ignored, and files are merged into bigger ones to use instead of a million smaller ones. Some PHP applications are even compiled into another programming language such as C++, which is much, much faster.

这也是一个编译/构建步骤。 测试文件将被忽略,文件会合并成较大的文件以供使用,而不是一百万个较小的文件。 某些PHP应用程序甚至被编译成另一种编程语言,例如C ++,速度要快得多。

Caching 快取

Caching is saving previously needed files and responses for later, with the expectation of them being requested again. If you ask the database for the total number of users in your database, it will count them and give you the number. If you make it save this number for later (that is, cache it), next time it is asked it can just grab the already prepared information. When you ask the server “What do I get if I visit mysite.com/user/5?”, it will tell you. If you tell it to remember the answer next time the question is asked, there’s no need for the server to look as it already knows. Caching is so important in web development―there’s a common saying that “cache is king.” It can mean the difference between life and death for your application when a huge surge of traffic suddenly happens.

缓存将保存以前需要的文件和响应以供以后使用,并期望再次请求它们。 如果您向数据库询问数据库中的用户总数,它将对他们进行计数并为您提供人数。 如果您将其保存下来供以后使用(即,将其缓存),则下次要求它仅获取已准备好的信息时即可。 当您询问服务器“如果我访问mysite.com/user/5会得到什么?”,它将告诉您。 如果您告诉它在下次询问该问题时记住答案,则无需让服务器看起来像它已经知道的那样。 缓存在Web开发中是如此重要-俗话说“缓存为王”。 当突然出现大量流量时,这可能意味着您的应用程序生死攸关。

Debugging is tightly coupled with performance. Besides a nasty error screen for your users, a bug in the code can cause execution locks, holdups in your code where there should be none, repeated and unnecessary queries into your database, and more.

调试与性能紧密相关。 除了给用户带来讨厌的错误屏幕外,代码中的错误还会导致执行锁定,代码中不应存在的保持,对数据库的重复和不必要的查询等等。

So, how does one measure performance or find bugs? There are many tools for profiling PHP applications (that’s what finding bugs and measuring the performance of various aspects of your application is called). Two of the better ones are Z-Ray and Blackfire (we won’t be covering them in this book as they are outside of its scope).

那么,如何衡量性能或发现错误呢? 有许多用于分析 PHP应用程序的工具(这就是所谓的查找错误并评估应用程序各个方面的性能的工具)。 更好的两个是Z-RayBlackfire (我们不会在本书中介绍它们,因为它们不在其适用范围之内)。

警告:当心微优化 (Warning: Beware Micro-Optimization)

It’s important to note that a common newbie error is micro-optimizing. For example, it was once believed that using single quotes with strings ($var = 'Some String') was faster than double quotes ($var = "Some String"). The performance gains such an optimization can bring to the table are negligent and almost always insignificant; instead, improving a complex SQL query or caching a remote HTTP call will always be an order of magnitude greater. When in doubt, use benchmarks and real data (such as those from Z-Ray or Blackfire), and never your gut.

重要的是要注意,常见的新手错误是微优化。 例如,曾经认为使用带字符串的单引号( $var = 'Some String' )比使用双引号( $var = "Some String" )更快。 这样的优化所带来的性能提升是疏忽的,几乎总是微不足道的。 相反,改进复杂SQL查询或缓存远程HTTP调用总是要大一个数量级。 如有疑问,请使用基准和真实数据(例如来自Z-Ray或Blackfire的基准)和真实数据,而不要直言不讳。

摘要 (Summary)

In this chapter, we explored the application environment, covering the various ecosystems present around your application in a given phase of its life cycle. We talked about virtual hosts and configuring your computer to redirect website URLs to your own PHP installation rather than looking for results online, and we discussed the all-important development/production parity.

在本章中,我们探索了应用程序环境,涵盖了应用程序生命周期中给定阶段中围绕您的应用程序存在的各种生态系统。 我们讨论了虚拟主机,并配置了计算机以将网站URL重定向到您自己PHP安装,而不是在线查找结果,并且我们讨论了非常重要的开发/生产对等。

If it all seems overly complex, don’t despair. This is only because we’ve dealt almost exclusively with theory so far―theory that is necessary to bravely proceed into the practical realm. In the following chapter, we’ll get our hands dirty with some virtual machines.

如果这一切看起来过于复杂,请不要绝望。 这仅仅是因为到目前为止,我们几乎只涉及理论,而理论是勇敢地进入实际领域所必需的。 在下一章中,我们将动手操作一些虚拟机。

对于那些想要更多的人 (For Those Who Want More)

In the application universe, testing the individual components is referred to as unit testing―the testing of each individual set of code so that you know it works.

在应用程序领域中,测试各个组件称为单元测试 -对每个单独的代码集进行测试,以使您知道它可以工作。

For example, a part of your application might have the ability to remove local symbols from every name and turn them into US-friendly letters. My last name “Škvorc” would thus be turned into “Skvorc.” Turning Škvorc into Skvorc is a small set of code, or a unit. This unit is testable; that is, for any given input of “Škvorc” I expect an output of “Skvorc.” I can then write a unit test, which is a file that defines the input and desired output, and when I run it, it tests whether or not this functionality still works. If, two months later, I change something in my application, I can easily run this test (which is still there) and check that this conversion still works. This sort of workflow ensures that you can upgrade your application later on without fear of breaking something you built before and forgot about. In our smartphone analogy at the beginning of the chapter, a single testable unit can be the touchscreen, or the battery.

例如,您的应用程序的一部分可能能够从每个名称中删除本地符号,并将其转换为对美国友好的字母。 我的姓氏“Škvorc”将变成“ Skvorc”。 将Škvorc转换为Skvorc只是一小段代码或一个单元 。 这个单元是可测试的 ; 也就是说,对于任何给定的“Škvorc”输入,我希望输出为“ Skvorc”。 然后,我可以编写一个单元测试 ,它是一个定义输入和所需输出的文件,当我运行它时,它将测试该功能是否仍然有效。 如果两个月后,我在应用程序中进行了更改,则可以轻松运行此测试(仍然存在)并检查此转换是否仍然有效。 这种工作流程可确保您以后可以升级应用程序,而不必担心破坏以前构建的东西而忘记了。 在本章开头的智能手机类比中,单个可测试单元可以是触摸屏或电池。

翻译自: https://www.sitepoint.com/php-application-environment/

php 桌面应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值