php查询类的方法总结

吾爱主题 阅读:134 2022-08-13 11:37:00 评论:0

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在php中,可以使用get_class_methods()函数来查询类里面有哪些方法。

get_class_methods()函数可以获取类的所有方法,返回由类的方法名组成的数组。

语法:

?
1 get_class_methods( $class_name )

$class_name:类名或者对象实例。

返回值:返回由 $class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 null。

示例:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <?php   class myclass {      // constructor      function myclass()      {          return (true);      }        // method 1      function myfunc1()      {          return (true);      }        // method 2      function myfunc2()      {          return (true);      } }   $class_methods = get_class_methods( 'myclass' ); // or $class_methods = get_class_methods( new myclass());   foreach ( $class_methods as $method_name ) {      echo "$method_name<br>" ; }   ?>

输出结果:

myclass
myfunc1
mufunc2

实例扩展:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <?php   class user   {    var $usertable ;    function get_oneuser( $field , $value )    {     $field_array = array ( "id" , "name" );  //查询方式     if (in_array( $field , $field_array ))     {      $sql = "SELECT * FROM `$this->usertable` WHERE $field='$value'" ;      $db = new database;      $res = $db ->execute( $sql );      $obj_user =mysql_fetch_object( $res );      return $obj_user ;     }     else echo "查询方式不对" ;    }    function get_moreusers()    {     global $db ;     $argnums =func_num_args();     $argarr =func_get_args();     switch ( $argnums )     {      case 0:       $sql = "SELECT * FROM `$this->usertable`" ;       break ;      case 2:       $sql = "SELECT * FROM `$this->usertable` WHERE $argarr[0]='$argarr[1]'" ;       break ;      case 4:       $sql = "SELECT * FROM `$this->usertable` WHERE $argarr[0]='$argarr[1]' AND $argarr[2]='$argarr[3]'" ;       break ;     }     //$db=new database;     $res = $this ->execute( $sql );     $obj_arr = array ();     while ( $obj =mysql_fetch_object( $res ))     {      $obj_arr []= $obj ;     }     return $obj_arr ;    }   } ?>

到此这篇关于php查询类的方法总结的文章就介绍到这了,更多相关php怎么查询类里面有哪些方法内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.php.cn/php-ask-487262.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容