类是变量与作用于这些变量的函数的集合。使用下面的语法定义一个类: 
<?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;
       }
   }
}
?>  

解决方案 »

  1.   

    多谢kingerq(多菜鸟)兄的辛劳和坦诚,小弟发“请资深人士开讲座”系列帖子的本意是想和大家一起探讨一些PHP中常见的问题和难点,这其中小弟也了解一些理论和实作技巧,但对于深层次的东西,我还想学习得更多,我想这也是大家共同的心愿。
    PHP手册是个好东西,但是相比起来实作的经验和相互的交流更重要,上CSDN的朋友未必水平都一样,熟悉和精通的方向也不一样,开一系列集思广益的帖子,高手们多上一些实作的程序和个人的心得、体会、技巧,我认为这对大家都是有帮助的。
      

  2.   

    <?php
    class upload
    {
    // class variable

    var $file_permitted; // array MIME type of the permetted file
    var $archive_dir; // upload directory
    var $max_filesize; // max size of file upload
    function upload ( $file_perm , $max_fil = 300000, $arc_dir = ".." )
    {
    if ( empty ($file_perm) ) $file_perm = array ("image/pjpeg","image/x-png","image/jpeg","image/png");

    $this->file_permitted = $file_perm; // set mime/type of files to upload

    $this->max_filesize = $max_fil; // set max size of file upload

    $this->archive_dir = $arc_dir;  // set destination dir of the upload file
    }
    function putFile ( $file )
    {
    $userfile_type = strtok ( $_FILES[$file]['type'], ";"); // clear file type:  MIME TYPE
    $userfile_name = $_FILES[$file]['name']; // set the original file name
    $userfile_size = $_FILES[$file]['size']; // set the file uploaded dimension
    $userfile = $_FILES[$file]['tmp_name'];  // file uploaded in the temp dir
    $userfile_rename = time();
    $error = "服务器不接受 $userfile_type 类型的文件<br>"; // set the error message
    $file_permitted = $this->file_permitted;
    $error = "";
            if ( ($userfile_size <= 0) || ($userfile_size > $this->max_filesize) ) $error = "File size error: $userfile_size Kb.<br>";
            if ( $error == "" ) if ($userfile_rename){
    $file_arg = explode(".", $userfile_name); // [1] is ext
    $filename = $userfile_rename .'.'. $file_arg[1];
    }else{
    $filename = basename ($userfile_name); // extract file name
    }

    if (!empty ($this->archive_dir) ) {
    $destination = $this->archive_dir."/".$filename; // full destination path to images dir
    }else {
    $destination = $filename; // full destination path to images dir
    }
    if(!is_uploaded_file($userfile)) return false;
    if ( !@move_uploaded_file ($userfile, $destination) ) return false;
    return $filename;
    }
    else
            {
             //echo $error;
    return false;
            } }
      

  3.   


    function chkgd2()
    {
    $testGD = get_extension_funcs("gd"); // Grab function list
    if (!$testGD) 

    echo "GD not even installed."; 
    return false; 
    }
    else
    {
    ob_start(); // Turn on output buffering
    phpinfo(8); // Output in the output buffer the content of phpinfo
    $grab = ob_get_contents(); // Grab the buffer
    ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering 

    $version = strpos  ($grab,"2.0 or higher"); // search for string '2.0 or higher'
    if ( $version ) return "gd2"; // if find the string return gd2
    else return "gd"; // else return "gd"
    }
    }
    function resize_jpeg( $image_file_path, $new_image_file_path, $max_width=480, $max_height=1600 )
    {     $return_val = 1;
         if(eregi("\.png$",$image_file_path)) // if is a png
         {
    $return_val = ( ($img = ImageCreateFromPNG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0";
         }     if(eregi("\.(jpg|jpeg)$",$image_file_path)) // if is a jpg
         {
         $return_val = ( ($img = ImageCreateFromJPEG ( $image_file_path )) && $return_val == 1 ) ? "1" : "0";
         }
         $FullImage_width = imagesx ($img);    // original width
         $FullImage_height = imagesy ($img);    // original width
         $ratio =  ( $FullImage_width > $max_width ) ? (real)($max_width / $FullImage_width) : 1 ;
         $new_width = ((int)($FullImage_width * $ratio));    //full-size width
         $new_height = ((int)($FullImage_height * $ratio));    //full-size height     //check for images that are still too high
         $ratio =  ( $new_height > $max_height ) ? (real)($max_height / $new_height) : 1 ;
         $new_width = ((int)($new_width * $ratio));    //mid-size width
         $new_height = ((int)($new_height * $ratio));    //mid-size height
         if ( $new_width == $FullImage_width && $new_height == $FullImage_height ) copy ( $image_file_path, $new_image_file_path );
    $gd_version = ( $this->chkgd2() );

    if ( $gd_version == "gd2" ) 
    {
         $full_id =  ImageCreateTrueColor ( $new_width , $new_height ); //Crea un'immagine
         ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
    }
    elseif ( $gd_version == "gd" )
    {
         $full_id = ImageCreate ( $new_width , $new_height ); //Crea un'immagine
         ImageCopyResized ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
    }

    else "GD Image Library is not installed.";

         if(eregi("\.(jpg|jpeg)$",$image_file_path))
         {
         $return_val = ( $full = ImageJPEG( $full_id, $new_image_file_path, 80 ) && $return_val == 1 ) ? "1" : "0";
         }     if(eregi("\.png$",$image_file_path))
         {
    $return_val = ( $full = ImagePNG( $full_id, $new_image_file_path ) && $return_val == 1 ) ? "1" : "0";
         }     ImageDestroy( $full_id );
         return ($return_val) ? TRUE : FALSE ;
    }


    function miniatura ( $image_path, $path, $larghezza=80, $altezza=80, $pre_name="thumb_" )
    {
    if ( (eregi("\.png$", $image_path) || eregi("\.(jpg|jpeg)$", $image_path)) && $image_path )
            {
    $image_name = basename ( $image_path );
                
    if (!empty ($path) ) 
    $thumb_path = $path."/".$pre_name.$image_name; // complete path to thumbnail dir
    else 
    $thumb_path = $pre_name.$image_name; // complete path to thumbnail dir

                if ( $this->resize_jpeg($image_path, $thumb_path, $larghezza, $altezza) ) return $thumb_path;
    else return "Errore nella creazione della miniatura<BR>";
    }
            elseif ($image_path) return "Impossibile creare la Miniatura da questo tipo di file<BR>";
    elseif ($send && $image_path) return "<font face=\"Verdana\" size=\"2\" color=\"red\"><b>Errore nel caricamento dell''immagine $cont</b></font><br>";
    }

    function splitFilePath ( $completePath = "null/null" ) {

    $filePath = array ( path=>"", filename=>"" );
    $tok = strtok($completePath,"/");

    while ($tok) {
    $file_name = $tok;
         $tok = strtok("/");
    } // end while

    $path = str_replace ( $file_name, "", $completePath );
    $filePath[path] = $path; 
    $filePath[filename] = $file_name; 
    return $filePath;
    }
    }
    ?>
      

  4.   

    php4里面的类:
    访问限定词:
    php4中的类里面的方法和属性没有访问权限限制词,全部都是public,这样我们在书写类的时候将一些不希望外界调用的方法或属性可以特别命名,以防止调用(如Private_ResourceID),尽量不要直接存取属性,而要写一些getXXX,setXXX的方法调用
    构造函数:
    最好让每个类都有自己的构造函数,做一些类的初始化工作,另外构造函数里面最好不要调用其他自定义函数,这样就可以使一个构造函数仅依赖于类本身,有利于代码移植
    个人认为,对于不大的项目,可以不考虑使用类,而直接使用面向过程的书写方式,这样对提高效率非常有好处
      

  5.   

    楼主很有心哦,表示支持一下.专题好多,没办法一个一个跟过来,先跟这一个吧.个人感觉,楼主的意思是这样,不是需要大段的文字,而只是希望大家把使用中的心得,
    容易犯的错误写下来一起探讨----不知道在下想的对不对.个人对于类的使用习惯.1 类尽量不要写的大而全,尽可能分散来写,一个类集中解决一个小问题.
    2 在类里面也可以引用别的类(好象是只要不循环引用就可以),非常方便.
    3 许多个小类拼在一起,解决的小问题可以变成解决一个大问题的类.
    4 根据第3点,等你手上小类多了,可以写一个'类的类',就是专门组合使用小类的类
    5 类里面也可以引用函数,跟引用类一样方便
    6 在同一个类里面,一个方法可以调用另一个方法,所以每一个方法应尽可能写的通用,精简
    7 预设值是个好东西,但应该在定义变量的时候,把预设值放到后面一点.
    8 要学会给自己的类写文档,并且养成将类,函数统一存放的习惯.
    写了一点,再次申明,在下也知道我写的东西对于高手来讲是很可笑的,也许这里面也有不对的
    地方.但凭心而论,这都是在下在实际学习过程中吃了不少苦头才体会到的.如果新手能因之而
    少走一点弯路,那就是我的荣幸了.
    另,再次感谢CSDN的各位朋友们,是大家帮我从一个不懂PHP的人到现在对PHP多少有一定的了解,
    把自己的一些体会写下来,也算是对CSDN的一点回报吧
    (有点借楼主的版面乱说的味道了,楼主不要见怪.)
      

  6.   

    接口.......,可以这么说,没有接口,就不叫面向对象.
    翻翻设计模式,基本每种模式都离不开接口.
    只可惜,直到php5才有接口,而且php5和php4也很难兼容,因此才使得很少谈论php的设计模式,但基本的设计思想是,接口 -> 抽象类  -> 普通类,
    还有一点,要说的就是,类的继承要满足的最基本原则:里氏代换原则,简单的讲,就是一个类如果从某一类继承,必需要满足在任何需要其父类的情况下,都可用其代替
    也就是说,类的继承应该是增加某种特性,而应保持父类的所有特性,
    打个比方,白马可以从马这个类继承,却不可以从黑马这个类继承
      

  7.   

    还没用过PHP5,不知道PHP5有没有多态、重载
      

  8.   

    重载是有的,多态好像没有有吧,没用过PHP5中的多态,楼上的可以查查手册
      

  9.   

    要学类,最好去看看java书的前两章.然后就差不多了..
    个人意见>.
      

  10.   

    个人觉得java更适合与学习面向对象的基本知识:
    think in java:地球人都知道的一本书,网上可以免费下到
    Design pattens: 讲述设计模式最经典的一本书
    java与模式:咱们中国人写的讲述设计模式的一本好书
      

  11.   

    曾一段时间也用JAVA写过程序,但写出来的接口都自己用,自己引用的时候只要用javabean引用实体BEAN远程接口或是会话BEAN远程接口的方法就行了。不知道给别人用的要怎么写才好。
      

  12.   

    PHP5支持重载这里有一篇关于PHP4的面向对象的入门文章呵呵。。王婆一下http://yubeinet.com/surfchen/simple.class.html
      

  13.   

    在类里面定义的函数,是给自身用的。类对外只是实现一些功能并返回一些变量而已。这样说没错吧?
    我看上面的例子,好像所有的类内的函数,对于变量都是携带进来的,而不是global的,都是这样么?
      

  14.   

    个人感觉surfchen(冲浪)的文章真的适合初学者看,当初学面向对象的时候怎么就没有看见这篇文章呢?可以少走很多弯路啊。
      

  15.   

    http://www.phpe.net/
    这个里面讲的很详细,也很资深
      

  16.   

    反对一下这篇文章
    http://yubeinet.com/surfchen/simple.class.html只不过你写的函数太糟糕了:)用函数难道不可以实现像下面写的类一样的功能吗?也可以。类的用处不是在于这篇文章说的这样,其实话是没错,重用,但对具体文章来说,表达上还是有错误的。
      

  17.   

    发展到oop是对软件复用和开发效率的要求 另外,用面向对象也是目前最符合人类思维习惯的
      

  18.   

    推荐一个网站。http://i.vwind.com
      

  19.   

    to netstu(孤心):
    关于这篇文章http://yubeinet.com/surfchen/simple.class.html确实,完全可以用函数实现。。而且实现起来似乎更简单
    我这篇文章只是让初学者了解到类是怎么样,如何使用类。我写这篇文章是因为我在网上看到的大多数关于PHP的面向对象的入门教程过于复杂了,失去了入门的味道。让初学者很容易感到迷惑,就像我当初学PHP的面向对象的时候,因为没有面向对象编程基础,看网上的教程就好象在看天书,所以后来对面向对象有了些许了解,就萌生了写这篇文章的念头。。
    就好象文件名一样,是simple.class
    当然也不排除有那些更容易理解复杂的人。
      

  20.   

    关于这篇文章http://yubeinet.com/surfchen/simple.class.html讲解类还不错。不过有一个疑问。
    $surfchen->ChangePhone("13117601514");//修改手机号码
    这一行用的是成员函数吧。如果构造函数本身会执行一些冬冬的话,而你只需要修改号码,开始时要先new定义一下,而在定义的时候岂不就会运行构造函数而可能执行一些东西么?用Person::ChangePhone("13117601514");会不会好点?
      

  21.   

    关于这篇文章http://yubeinet.com/surfchen/simple.class.html这篇文章对于初学者了解类是有一定帮助的。netstu(孤心) “用函数难道不可以实现像下面写的类一样的功能吗?”
    类的方法和函数并无本质的区别,只是他们所处的层次不同罢了zairwolfi(君子兰) “用Person::ChangePhone("13117601514");会不会好点?”
    如此调用类的方法并不能改变类的属性,所以并不能实现你说的目的对象是什么?对象是具有一些特定功能的实体
    类是什么?类是对象的抽象描述
      

  22.   

    很多大的程序没用任何面向对象的东西,依然写的很棒:phpMyAdmin很多代码,完全面向对象,使其有完美的可扩展性: xoops这些都是php的经典程序
      

  23.   

    呵呵个人觉得类的设计主要就是两个问题,
    第一,就上需不需要类的问题,主要就是看这个类是不是具有很好的重用性,抽象性
    第二个,就是类的设计一定要功能单一,类里面的方法也一样,还有构造函数不要太长
    还有,就是命名规则啦,这个很重要,Java类的命名规则就很不错,
    还有就是一些设计原则之类的东西啦,
    如开-闭原则,里氏代换原则,依赖倒转等基本原则,经管PHP很难做的这一点,最起码要慢慢向其靠拢吧;)
      

  24.   

    还有一个重要的原则:“不要和陌生人说话”,别笑,认真的;)
    就是一个类,不要和陌生的类直接交流,而只应该与其朋友交流,所谓的朋友,就是指,
    对象本身(this)
    以参数形式传入的对象
    当前对象的实例直接引用的对象
    如果当前对象实例是一个聚结,那么在一个聚集内的也应该算“朋友”(当然了php中不存在这个)
    当前对象所创建的对象
      

  25.   

    类其实没什么讲头。要讲将OOP。
    OOP有三个主要特征:封装、继承、多态。
    这三个搞懂了就懂很多东西拉。
      

  26.   

    其实无论学什么,初学者必须还是要搞清几个概念
    对象(控件)、属性
    事件、方法
    循环、判断分支
    赋值、变量类型如果随便给出一个关键字,你能说出它属于那种上述概念,才好去学其他组合出来的咚咚咔咔,想问一句,unset()语句应看作什么呢?我看作省略了对象的方法,比较好理解些……类,我是把它看作自定义对象,里面的function就看作该对象的方法(少量可看作属性)
    写的时候只不过把连接符号“.”换成了“->”而已
      

  27.   

    //unset()语句应看作什么呢?我看作省略了对象的方法,比较好理解些……
    unset()只能称为函数(function),与方法(method)完全不一样。//写的时候只不过把连接符号“.”换成了“->”而已
    C++中指针对象用->,otherwise用. ,这能称为'换成了->而已吗'?