首页 > 编程技术 > php

php入门教程(类实例教程)

发布时间:2016-11-25 15:10

这里主要讲到关于在php教程类的调用,申请明,使用以前各种类的实例方法与操作过程,让你清楚的知道php类的construct  destruct  clone  call wake sleep用法。

简单购物车类

<?php
class cart
{
    var $items;  // 购物车中的项目
    // 把 $num 个 $artnr 放入车中
    function add_item ($artnr, $num)
    {
        $this->items[$artnr] += $num;
    }
    // 把 $num 个 $artnr 从车中取出
    function remove_item ($artnr, $num)
    {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } else {
            return false;
        }
    }
}
?>

类中带有构造函数

<?php
class session
{
    public  function __construct()
{
     echo '构造函数执行';
    }
    public  function __destruct()
    {
      echo '析构函数执行';
    }
}
$obj = new session;
unset($obj);//删除对象, __destruct()会被调用的
//输出
//构造函数执行
/析构函数执行
?>

类的调用与clone

<?php
class session
{
 public $age = 20;
 public $sub = null;
 public function __clone()
 {
  $this->sub=clone $this->sub;
 }
}
class session 2
{
 public $value=5;
}
$s   = new session;
$s->sub=new session 2;
$s2 = clone $s;
$s->sub->$value =10;
echo $s2->sub->$value;
?>

类 call方法

<?php
class session
{
 protected function __call($func,$para)
 {
  echo '方法不存在:’func."n".'参数为:'."n";
  print_r($para);
 }
}
$s=new session ();
echo $c->getnames('hello','you');
/*
输出:
方法不存在:getnames
参数为:
array
(
    [0] => hello
    [1] => you
)
*/
?>

类wake sleep

<?php
//定义一个session类
class session {
 public $sessionvars;
public $id;
 function session() {
  $this->id = uniqid();
 }
 function _sleep() {
 }
 function _wake() {
  $this->id = uniqid();
 }
}
    //建立一个对象
    $s = new session;
    $s-> sessionvars = "1.02";

    //串行化
    $ss = serialize($s);
    //unserialize it 反串行化 id被重新赋值
    $s2 = unserialize($s);
    // $s和$s2有不同的id
    print_r($s);
    print_r($s2);
  ?>

对文件访问实现的源代码。主要讲到php教程操作文件以及xml文档实例方法。

<?php
// 创建一个新的pdf文档句柄
$pdf = cpdf_open(0);
// 打开一个文件
cpdf_open_file($pdf);
// 开始一个新页面(a4)
cpdf_begin_page($pdf, 595, 842);
// 得到并使用字体对象
$arial = cpdf_findfont($pdf, "arial", "host", 1);
cpdf_setfont($pdf, $arial, 10);
// 输出文字
cpdf_show_xy($pdf, "this is an exam of pdf documents, it is a good lib,",50, 750);
cpdf_show_xy($pdf, "if you like,please try yourself!", 50, 730);
// 结束一页
cpdf_end_page($pdf);
// 关闭并保存文件
cpdf_close($pdf);

// 如果要直接输出到客户端的话,把下面的代码加上

$buf = cpdf_get_buffer($pdf);

$len = strlen($buf);

header("content-type: application/pdf");

header("content-length: $len");

header("content-disposition: inline; filename=pie_php.pdf");

print $buf;

cpdf_delete($pdf);
?>

xml文件

<?php
$dom = new domdocument();
$dom->load("order.xsl");
$proc = new xsltprocessor;
$xsl = $proc->importstylesheet($dom);

$xml = new domdocument();
$xml->load('order.xml');
$string = $proc->transformtoxml($xml);
echo $string;
?>

order.sls

<?xml version="1.0" encoding='gb2312' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
<xsl:output encoding='gb2312'/>

  <xsl:param name="column" select="'sku'"/>
<xsl:param name="order" select="'ascending'"/>
  <xsl:template match="/">
    <html>
      <body>
        <xsl:apply-templates select="order">
          <xsl:with-param name="sortcolumn" select="$column" />
          <xsl:with-param name="sortorder" select="$order" />
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="order">
    <xsl:param name="sortcolumn" />
    <xsl:param name="sortorder" />
    <table border="1">
      <tr>
        <th>订单号</th>
        <th>id</th>
        <th>说明</th>
        <th>价格</th>
        <th>数量</th>
        <th>合计</th>
      </tr>
      <xsl:apply-templates select="item">        
      </xsl:apply-templates>
    </table>
  </xsl:template>
  <xsl:template match="item">
    <tr>
      <td><xsl:value-of select="../account" /></td>
      <td><xsl:value-of select="sku" /></td>
      <td><xsl:value-of select="description" /></td>
      <td><xsl:value-of select="priceper" /></td>
      <td><xsl:value-of select="quantity" /></td>
      <td><xsl:value-of select="subtotal" /></td>
    </tr>
  </xsl:template>    
</xsl:stylesheet>

order.xml文件

<?xml version="1.0" encoding='gb2312' ?>
<order>
  <account>9900234</account>
  <item id="1">
    <sku>1234</sku>
    <priceper>5.95</priceper>
    <quantity>100</quantity>
    <subtotal>595.00</subtotal>
    <description>www.111cn.net</description>
  </item>
  <item id="2">
    <sku>6234</sku>
    <priceper>22.00</priceper>
    <quantity>10</quantity>
    <subtotal>220.00</subtotal>
    <description>足球</description>
  </item>
  <item id="3">
    <sku>9982</sku>
    <priceper>2.50</priceper>
    <quantity>1000</quantity>
    <subtotal>2500.00</subtotal>
    <description>手机包</description>
  </item>
  <item id="4">
    <sku>3256</sku>
    <priceper>389.00</priceper>
    <quantity>1</quantity>
    <subtotal>389.00</subtotal>
    <description>手机</description>
  </item>
  <numberitems>www.111cn.net</numberitems>
  <total>3704.00</total>
  <orderdate>07/07/2002</orderdate>
  <ordernumber>8876</ordernumber>
</order>

 

 

函数和类来实现,包括计数,字符串连接,函数返    回结果,数字加法运算,数字乘法运算,数字大小排序等通过函数来实现的例子

<?php教程
function small_numbers()
{
    return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
?> 

函数引用

<?php
function &returns_reference()
{
    return $someref;
}
$newref =& returns_reference();
?>

相加减

<?php
function foo()
{
    $numargs = func_num_args();
    echo "number of arguments: $numargs<br />n";
    if ($numargs >= 2) {
        echo "second argument is: " . func_get_arg(1) . "<br />n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "argument $i is: " . $arg_list[$i] . "<br />n";
    }
}
foo(1, 2, 3);
?>

 

源代码主要用函数实现,包括计数,字符串连接,函数返    回结果等通过函数来实现的例子

函数

<?php教程
function mycount($invalue1,$invalue2)
{
  $addvalue = $invalue1+$invalue2;
  return $addvalue;     //返回计算结果
}
$count = mycount(59,100);
echo $count;     //输出159
?>

函数调用

<?php
function str_unite (&$string)
{
    $string .= '也喜欢蓝色.';
}
$str = '喜欢红色,';
str_unite ($str);
echo $str;    // 输出结果: '喜欢红色,也喜欢蓝色.'
?> 

函数返回值

<?php
function mycolor ($incolor = "蓝色")
{
    return "我喜欢的颜色: $incolor.n";
}
echo mycolor();
echo mycolor("粉色");
?> 


全局变量

<?php
$a = 1;
$b = 2;
function sum()
{
    global $a, $b;
    $b = $a + $b;
}
sum();
echo $b;
?>

一款简单数字计算

<?php
function square ($num)
{
    return $num * $num;
}
echo square (4);   // 输出'16'.
?> 

php入门教程第一课简单的PHP显示与嵌入PHP
 代码如下 复制代码
<html>
<head>
<title>第一个php教程程序</title>
</head>
<body>
<?php
echo "hello, 这是第一个php程序";
?>


</body>
</html>

实例二 html代码讲解范例

 

 

 

 代码如下 复制代码
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> html中嵌入php的例子</title>
</head>
<body>
    <p>我最喜欢的颜色:
        <?php  echo "红色"; ?>
</p>
</body>
</html>

 

标签:[!--infotagslink--]

您可能感兴趣的文章: