仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1386|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网页编程之[转]类与PHP (I)

[复制链接]
分手快乐 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:16:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
到现在,对排版还是不很熟练,经常会排不好。   Classes and PHP

Rod Kreisler

The hardest concept I've tried to understand since beginning to use PHP was that of classes. I'd never used a database engine but learning to use MySQL, at least for the more basic functions, was a breeze. Having never used OOP before, classes were novel as well, but understanding the theory and why it was useful escaped me. I knew they must be powerful as "everything" is programmed using OOP, but for the life of me, although I thought I understood the mechanics, I couldn't see the usefulness. Then, just a few days ago, while trying to figure out how to do something with regular functions it hit me just how doing it with objects would make my job much simpler! I'm going to try to explain about them in plain English and hopefully help others like myself.

Classes are nothing more than a collection of variables and functions acting on those variables. They provide a means of thinking about things in real world terms. In other words they describe an object. An object, or instance, of a class is an actual "living, breathing" structure of that class. Let's say we want to describe a bicycle. A proper class of a bicycle might have the variables $pedals, $chain, $front wheel, $rear wheel, $brakes, and $handle_bars. Functions of the bicycle would include Stop(), Accelerate(), Coast(), TurnLeft() and TurnRight(). You can think of your script as the entity operating that bike. The function Accelerate() could be passed an argument such as $Braking_Force and use that information along with the defined instance variables (probably $brakes and $wheels) and output some result back to your script.

Interesting, but couldn't all this be accomplished using regular variables and functions? Yes it could, and if you only had one bike in your script it probably wouldn't make much sense to define a class just for it. But what if you needed several bicycles? It could become quite complex keeping track of all those variables and making sure you pass the correct variables to the different functions, and using objects cuts down on the number of variables you need to pass because the function automatically has available to it all the variables describing the object the function is acting upon. Also, class definitions are easily included in different scripts and you'll be assured that a bicycle works the same way in each script!

Let's create a class that I actually use on almost every page on my site and you might find useful, too.

I don't know about you, but when I'm writing a dynamic web page I hate to have to stop thinking about the logical flow to worry about properly formatting my HTML. As a consequence of this, I often end up with not so attractive pages because I don't want to worry about font faces and sizes, or background and text colors. The solution: using a PHP class to set HTML output attributes with functions to format the text!

I call the class "Style". It contains the following variables that set important HTML attributes for formatting the output:

<?php

class Style {

    var $text;
    var $alink;
    var $vlink;
    var $link;
    var $bgcol;
    var $face;
    var $size;
    var $align;
    var $valign;

}

?>

I'm sure you're familiar with HTML so the variables should be self- explanatory. Next I created a function for Style called Style:

<?php

class Style {

function Style ($text="#000000",$alink="#AA00AA",
$vlink="#AA00AA",$link="#3333FF",
$bgcol="#999999",$face="Book Antiqua",$size=3,$align="CENTER",$valign="TOP") {

    $this->text=$text;
    $this->alink=$alink;
    $this->vlink=$vlink;
    $this->link=$link;
    $this->bgcol=$bgcol;
    $this->face=$face;
    $this->size=$size;
    $this->align=$align;
    $this->valign=$valign;

}

}
?>

   PHP和HTML混合编程应该不成问题,在这期间,你完全可以让PHP给你算算 一加一等于几,然后在浏览器输出,不要觉得幼稚,这的确是跟阿波罗登月一样,你打的是一小段代码,但是对于你的编程之路,可是迈出了一大步啊!兴奋吧?但是不得不再给你泼点冷水,您还是菜鸟一个。
小魔女 该用户已被删除
沙发
发表于 2015-2-4 10:26:24 | 只看该作者
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
冷月葬花魂 该用户已被删除
板凳
发表于 2015-2-9 22:06:55 | 只看该作者
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
愤怒的大鸟 该用户已被删除
地板
发表于 2015-2-23 10:45:27 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
小妖女 该用户已被删除
5#
发表于 2015-3-2 11:46:40 | 只看该作者
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
若天明 该用户已被删除
6#
发表于 2015-3-4 22:58:57 | 只看该作者
装在C盘下面可以利用windows的ghost功能可以还原回来(顺便当做是重转啦),当然啦我的编译目录要放在别的盘下,不然自己的劳动成果就悲剧啦。
老尸 该用户已被删除
7#
发表于 2015-3-6 06:51:58 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
因胸联盟 该用户已被删除
8#
发表于 2015-3-10 10:29:27 | 只看该作者
实践是检验自己会不会的真理。
9#
发表于 2015-3-12 07:55:20 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
小女巫 该用户已被删除
10#
发表于 2015-3-17 00:47:59 | 只看该作者
真正的方向了,如果将来要去开发团队,你一定要学好smarty ,phplib这样的模板引擎,
admin 该用户已被删除
11#
发表于 2015-3-23 09:46:54 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
活着的死人 该用户已被删除
12#
发表于 2015-3-27 05:45:59 | 只看该作者
建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。
不帅 该用户已被删除
13#
发表于 2015-4-5 05:44:25 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
莫相离 该用户已被删除
14#
发表于 2015-4-7 12:52:42 | 只看该作者
我还是强烈建议自己搭建php环境。因为在搭建的过程中你会遇到一些问题,通过搜索或是看php手册解决问题后,你会更加深刻的理解它们的工作原理,了解到php配置文件中的一些选项设置。
第二个灵魂 该用户已被删除
15#
发表于 2015-4-10 08:57:38 | 只看该作者
当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,
蒙在股里 该用户已被删除
16#
发表于 2015-4-12 02:58:25 | 只看该作者
我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:
飘灵儿 该用户已被删除
17#
发表于 2015-4-27 20:32:26 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
再现理想 该用户已被删除
18#
发表于 2015-4-30 23:24:49 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
谁可相欹 该用户已被删除
19#
发表于 2015-4-30 23:54:01 | 只看该作者
本人接触php时间不长,算是phper中的小菜鸟一只吧。由于刚开始学的时候没有名师指,碰过不少疙瘩,呗很多小问题卡过很久,白白浪费不少宝贵的时间,在次分享一些子的学习的心得。
飘飘悠悠 该用户已被删除
20#
发表于 2015-5-6 04:10:22 | 只看该作者
其实没啥难的,多练习,练习写程序,真正的实践比看100遍都有用。不过要熟悉引擎
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-3 02:12

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表