因为公司经常有PHP的外包项目做,客户指定使用PHP来开发,而我是Java组的,没做过PHP项目,心生好奇,趁今晚有空来摸摸PHP。
装了PHPeclipe和xampp,google到PHP手册。
一起就绪,学起PHP来,发现PHPeclipse的代码提示做的很不错。
可是慢慢看手册,我慢慢发现,都说PHP开发小型项目快,但是页面里嵌入一坨一坨的逻辑代码,非常不爽啊。
在看到MySql函数这章时,我已经完全受不了了!
<?php
// 连接,选择数据库
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');
// 执行 SQL 查询
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// 用 HTML 显示结果
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// 释放结果集
mysql_free_result($result);
// 关闭连接
mysql_close($link);
?>
数据库连接->查询->就在页面本地显示结果,加上古怪的语法、关键字和函数名,完全没有MVC的概念,简直连JSP都不如!
看来5年前板桥对PHP的批斗是万分正确的。
太无奈了!
我继续google,得到一份CakePHP Manual,以前没细看Cake,只了解它是Rails的盗版。
看看Cake Blog Tutorial上手吧。
结果PHPeclipse竟然不支持thtml文件的语法高亮、代码和标签提示!
郁闷之中,抱着忍者无敌的精神一行一行代码的敲,细细体会CakePHP开发是有多Rapid。
posts_controller.php:
<?php
class PostsController extends AppController
{
var $name = 'Posts';
function index()
{
$this->set('posts', $this->Post->findAll());
}
function view($id = null)
{
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
function add()
{
if (!empty($this->data))
{
if ($this->Post->save($this->data))
{
$this->flash('Your post has been saved.','/posts');
}
}
}
function edit($id = null)
{
if (empty($this->data))
{
$this->Post->id = $id;
$this->data = $this->Post->read();
}
else
{
if ($this->Post->save($this->data['Post']))
{
$this->flash('Your post has been updated.','/posts');
}
}
}
function delete($id)
{
$this->Post->del($id);
$this->flash('The post with id: '.$id.' has been deleted.', '/posts');
}
}
?>
index.thtml:
<h1>Blog posts</h1>
<p><?php echo $html->link("Add Post", "/posts/add");?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $html->link($post['Post']['title'],"/posts/view/".$post['Post']['id']); ?></td>
<?php echo $html->link('Delete', "/posts/delete/{$post['Post']['id']}", null, 'Are you sure')?>
<?php echo $html->link('Edit', "/posts/edit/".$post['Post']['id']);?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
哇,这代码太华丽了!太精湛了!
$this->Post-> 、<?php..?> 、echo 和$post['Post']['xx'] 写到手软,真服了,什么烂语法啊!
CakePHP的作者你们辛苦了,为了赶Rails的时髦让你们以及使用Cake的广大软件作坊的同仁们敲烂无数键盘,硬件商要感谢你们!
你们是伟大的功臣!来自火星的英雄!
PHP、CakePHP,地球不适合你们。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com