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

PHPer下一步应该学go?

其实看到现在,很多人都在推荐go语言,理由有很多种,但对于PHPer来说,不一定就是适合的。go是google推出的语言,到现在为止也已经有很长时间了,再加上go又能底层,又能WEB,所以。。。
但是,究竟应该不应该学,还是会有点想法,所以看到这篇全英文的介绍的时候就蛋定不了了。
原文是来自于:Why you PHP guys should learn Golang

Go or Golang, is a open source, community supported, fast, consistent, scalable, productive language, and conceived by Google. More and more applications were build with it. Although Rob Pike has said that “… we expected C++ programmers to see Go as an alternative …”, but I really think of that: You PHP guys should learn Golang! Let’s talk about WHY.

 

Easy to learn

PHP is easy to learn. Golang is as easy as PHP!

The syntax of PHP is similar to C family’s programming languages(C/C++/Java etc.). If you have some experiences with those languages, it is not too hard to write some codes when you first met PHP.

Golang is also a C family’s programming language. Eh… with a litte syntax difference. For example, the keyword “for” is similar with PHP’s, but without the pair of parentheses. And the condition keyword “if” is without the pair of parentheses either. You can read Effective Go for more things.

Golang has only 30 keywords and 47 operators, delimiters or other special tokens. There is no need to work hard for remembering all the literals. The type system is exquisite and easy to use. No huge OO but a practical struct type with methods on it. The interface design is one of my favorate parts of Golang. When you have finished this A Tour of Go, based on your PHP experiences, some jobs could be done with Golang immediately.

Easy to Use

Your PHP scripts will be evaluated by SAPI components: web server module, PHP-fpm or CLI. All needed for PHP deploying is a SAPI environment. Deployment of the environment is the most difficult part to understand for a PHP beginner.

All the Golang’s codes will be compiled and linked into native code. So there is no need to deploy a environment for it executing but a build environment. Compaire with deployment of the PHP environment, it is not so much complex. Do you really think of the deployment of the PHP environment is really complex? I don’t think so. Really!

To deploy the build environment, just need 3 steps:

  • Download Golang‘s source code.
  • Setup the environment variables. Here’s a great blog post: Go environment setup
  • Run the ‘all.bash’ in the ‘src’ directory.

Or only one step: Use the binary package to setup.

Then a toolset called “go” can be used. Using the go tool is as easy as using PHP’s CLI tool. “The Go Tool” has already explained it.

A myth of PHP

If a programming language is easy to learn and use, shall we learn and use it? Lots of programming languages are easy to learning and use. Shall we learn and use all of them? The answer is straightforward: NO!

But why you PHP guys should learn Golang? Just because it is cool! Ja, I’m kidding, but it is true. Anyway, let’s talk about PHP itself first.

PHP is “a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages.(Wikipedia)”. The most important feature of PHP is embedding with HTML. PHP code write in “<?php … ?>” tag; HTML write outside. It also has a powerful extension system. The extension can be writen by C with Zend API. Data processing was actually done by those extensions. IMO, PHP must be the best template programming language.

But if you have years experiences with PHP and began face to some complex web application. You must have already felt some powerlessness to PHP. It dosen’t have concurrent mechanism build-in, no threads, multiprocessing or X-routines. A slow data source will be blocked entire page’s processing. A powerful web server in the front of PHP is always needed. Backend programes were writen to process data. Message Queue, Cache, Proxy … The system is not only about PHP, but also about a lot of services and system components. This time, PHP is really be a template language with few logical jobs.

PHPers are always looking for solutions, like “PHP multithread” or a concurrent RPC framework for PHP. I could not say witch one is better. But I’m sure that you must choose an other programming language for the backend job. In my own case, I tried C(struggle with malloc/free)/Java(stumble in the jar hell)/Python(never be pythonic but type lost)..If I get profermance, I must struggle with the memory management; If I use GC, I must deploy and turning VM; If I get convenient, careless would kill me….Eche has advantages, each also has a problem.

OK! Let’s back to Golang now!

Golang has GC, there is no need to care about memory management(or could pay less attention to it). And the code are compiled into native machine code. So, “cp” and “mv” are all needed for deploying the application writen by Golang. Oh, as I just said, Golang is a compiled language with a static type system. You have no chance to confuse veriable types. Of course, one of the most important thing is PHPer who moves to Golang “don’t have to surrender much expressiveness, but gain performance and get to play with concurrency(by Rob Pike)”. “Why Not Go?” talked about this deeply.

I would show parts of my own experience: a Gearman worker works for us to process backend data. PHP connected to the worker though Gearman Job Server with its Gearman API. The original worker was writen by python. There are lots of problems with that version(They are our own problems, but python’s). But it works for us. Then we rewrite the worker with Golang. It was amazing. I wrote a Gearman API for Golang, and a Golang package using Zend API(cgo is another amazing thing) for evaluating the PHP script in Golang. Then combined them together – A Gearman worker witch can execute PHP scripts. It works about 3 mounth in our production enverionment and looks great! Oh, I also wrote a RPC-combinator for combining the RPC call in PHP script. It will be used in our next product.

Everything goes right. Thanks Golang!

In any case, most of PHPers alwasy need to learn other languages for the backend development. If you have began looking for, or already tasted some of other languages. Why don’t you try Golang? It would make your life happy and easy. You could spend more time with your family and friends, eat your favorite foods and visit the places you love.

That’s why you PHP guys should learn Golang! Is it enough? I think so!

This is my writing exercise. If you find any grammatical or spelling error, plz tell me!

---EOF---

其实我也挺纠结的,要知道当时我刚跟着tour做了30多篇的时候,突然golang的网页就再也打不开了。再苦了,墙就是我们学习的最大障碍。想想这些年我们遇到了什么。。
sf.net被墙
groups.google.com被墙
python.org被墙
很多。。。。。这些都是我们的障碍啊

LAMP练级指南

亲,玩过大波罗没。玩过魔兽没,那么应该了解技能树的一些特征吧。
当一定的等级达到的时候,可以新开一个技能树。然后已经点亮的技能树可能还要升级,因为前面的技能树也可能会需要初级技能的升级版。

说的有点乱了。NND,直接复制一份百科的技能树的说明算了:(靠,百科居然没有。。。。连知识树也没有,只能用企业知识树模型来代替了):

XML/HTML代码
  1. 企业知识树模型本质上就是一个层级式知识图,它表达了为实现某一组织目标的所有相关组织知识间的因果关系或从属关系。层级式知识图由于具有对称性、良好图形、连续性等特点,而得到广泛的应用。企业知识树模型在层级式知识图的基础上拓展了更多的管理功能。考虑一个为完成某一组织目标的知识构成的层次知识结构。它是若干个关联的知识结点集K,知识结点间的关系集r,构成了一个关系集R。  

意思差不多。。。
下面的文章就是来自于对LAMP技能的知识树的的提炼。您看好了:(http://blog.cunsheng.me/?p=42)

俗话说温故而知新,也正好培训需要,我最近也整理了一张LAMP程序员技能树,相比我所见过的技能树Flash开发者,这张图有以下特点:

  1. 由于同一门知识在不同阶段有不同的要求,为了具有更好的参考性,技能树将所有知识和技能点按1、初中级(工作1-2年)2、中高级(工作3-4年)和3、高级(工作3年+)分成三级, 但除了网络与架构外其余的知识和技能点都只罗列到了第二级,原因是大多知识和技能到了一定程度后,都着眼于非常具体、非常细节的问题,很难全面罗列,也可 能也因为我自身有限的眼界,至于网络与架构有第三级倒不是因为这些知识有多高深,而是相比其余知识,这些知识可以晚一些再掌握
  2. 知识点附带资料与书籍推荐,蓝勾表示有网页资料,绿手表示有书籍推荐(查看flash版可直接链接到资料页),毕竟每个知识点只是简单几个字,人背景不同理解也不同,对着资料或参考书籍来看能更好的表达我的意图,同时也更能帮助到所需之人
  3. 全面性是这棵技能树的主要考虑之一,但不得不承认要完全掌握所有内容是非常不容易的事情,有些理想化,现实中往往还要考虑如你上级的要求、公司的需要等实际因素,能作为checklist发现问题,能让读者更了解自己的能力现状,那么制作技能树的目的也就达到了

由于技能树涉及到多种编程语言,所以把语言的共通部分以及编程基础单独提炼了出来:

http://cunsheng-wordpress.stor.sinaapp.com/uploads/2012/08/LAMPer%E6%8A%80%E8%83%BD%E6%A0%91.swf

------

值得一观啊

 

在新浪SAE上使用htmlpurifier

修复HTML页面,以前一直用的是tidy庫,但SAE上其實是無法使用的

所以,我選擇了htmlPurifier庫,不過如果直接使用的話,是會報錯的,因為HtmlPurifier默認在修復頁面的時候會使用緩存,這樣可以加快速度。因此代碼就變成了這樣

PHP代码
  1. $htmlPurifier = new HTMLPurifier(array(  
  2.         'AutoFormat.AutoParagraph' => TRUE,  
  3.         'HTML.TidyLevel' => 'medium',  
  4.         'Cache.DefinitionImpl' => NULL,  
  5.     ));  

本来,我是将Cache.DefinitionImpl设置为SAE_TMP_PATH的,但HTMLPurifier中会chmod这个目录的权限,而tmppath是无法设置权限的,因此我干脆设置为NULL。

这样一来,问题就解决了。哈哈

Tags: sae, tidy

一件挫事

之前在給數據做緩存的時候做了一件挫事:
將某個數據表進行了cache,數據索引的鍵是該表的某個字段 。這個數據我是一般不會動它的,但是我需要知道這個表中最大的xxx_ID(索引字段)是什麼。
當然是有兩個辦法:
1、select max(xxx_id) from table
2、直接將緩存中最大的xxx_ID取出來就完事了

我用的是第二種,因為我order by 的時候是xxx_id ASC,於是我想當然的就直接array_keys($data),然後array_pop一下,最後彈出的這個xxx_ID就是最大的xxx_ID

但理想往往都是與現實相反的,不知道為什麼,它就是沒按順序排。。於是我array_pop死出彈不出我要的數值。
想了半天,突然想到。。。max不就完了嗎?於是蛋疼N久的問題一下子就解決了。何苦那麼累呢。哎。。。

腦子真的有時候是轉不過彎啊

最小完美哈希函数

不好意思,这是一个标题党,主要是这个词语太让人震惊,但现在它又将变的地了。所以我先记录一下。

什么是完美哈希函数?
這一段內容來自:(http://www.kuqin.com/algorithm/20111108/314569.html)
最小完美哈希函数是什么,要从定义说起,这个名字很长,一步步解释。

  1. 哈希函数 任意函数h(x)都可以说哈希函数,一般来说,一个良好的哈希函数可以尽量避免重复。x的集合是参数域,h(x)的集合是值域。
  2. 完美哈希函数  完美哈希函数,就是完全不会冲突的哈希函数,这要求函数的值域至少比参数域要大
  3. 最小完美哈希函数 最小完美哈希函数,就是指函数的值域和参数域的大小完全相等,一个也不多
  4. 保序最小完美哈希函数 保序的意思就是指这个哈希之后顺序是不变的,同时还能满足其他两个条件。
这个函数的优点就是形式上很完美,就像给一个排好序的文档编上的序号一般紧凑可靠。但是这个函数有两个缺点,一是必须事前必须知道原数据集,二 是需要花一定的CPU来生成这个函数。我认为,对于数据仓库类的线下搜索应用,这个算法是有用武之地的。但对于强调实时的数据业务,这个算法是不适合的。
----------
這裏還有一個點評:(http://liulixiang.info/blog/tag/%E6%9C%80%E5%B0%8F%E5%AE%8C%E7%BE%8E%E5%93%88%E5%B8%8C%E5%87%BD%E6%95%B0/)

点评:(by liulixiang)

“最小完美哈希函数”从概念上很完美,生成的思想也很不错,不过实际运用中并不是那么常见:应用中已知数据集再做哈希的情况不是特别多,对时空的复杂度要求也没有过于苛刻,普通的方法即可承受。另外一般数据集很大,生成该函数的开销也太大。

---------
參考文檔:

最小完美哈希函数方法来自于Czech, Havas, Majewski等的论文:

An optimal algorithm for generating minimal perfect hash functions(Czech等1991)

Graphs, hypergraphs and hashing(Havas等1993)

A family of perfect hashing methods(Majeki等1996)

本文大部分转述或摘抄自《深入搜索引擎——海量信息的压缩、索引和查询》(该书豆瓣链接) 一书(Page 173~181),该书由梁斌翻译(原版即大名鼎鼎的MG——“Managing Gigabytes: Compressing and Indexing Documents and Image, Second Edition” 由Ian H. Witten, Alistair Moffat, Timothy C.Bell合著)(MG网站