drqn5418 2010-12-14 11:00
浏览 25
已采纳

PHP类是正确的

still getting used to PHP classes so a bit of "help /guidance" please

I have a class like this:

class ansa_accounturl_query {

    function __construct() {

     global $DBH;
     global $limit;
     $STH = $DBH->query("SELECT frm_url.frm_urlID,frm_url.frm_url FROM frm_url WHERE frm_url.accountID='SOMETHING' ".$limit." ");
     $STH->setFetchMode(PDO::FETCH_OBJ);  
     $this->noforms = $STH->rowCount();  
     while($row  = $STH->fetch()): 
         $this->frm_urlID[]  = $row->frm_urlID;
         $this->frm_url[] = $row->frm_url;
     endwhile;
    }

}

The limit comes from a PHP function - and works.

What I would really like to do is create extend classes for the above example which gives say $this-frm_url as a function. so that on the "page" I call the class $classcall = new class(); but rather than have to go echo $classcall->frm_url[$key]; I can just call a function like this echo frm_url(); So in the example above there would be 2 extend classes one for frm_urlID and one for frm_url.

Also, in the "master class" am I right in in setting as array? i.e. $this->frm_url[] as without that I cannot seem to run a loop but the loop does seem "over" complex if you do it this way as you (well I) have to get a count of the array items then run the loop so you (again I) have a for() statement then a foreach(). Seems longwinded to me.

Thanks in adavance.

  • 写回答

3条回答 默认 最新

  • dongtiaobeng7901 2010-12-14 11:17
    关注

    First: Please do not use globals. If you have to use "global" there is a 90% percent chance that your design is bad. Either pass $DBH and $limit as parameters to __construct($dbh,$limit) or define them as static propertys of ansa_accounturl_query. If you define them as static propertys the values will still be identical for all instance of your class.

    Second: If you want to call a method without creating a instance first you can declare the methods static, too. Then you can call them like this:

    classname::myMethod(parameter);
    

    if you allways use the same db and the same setting I would suggest you create a class with static propertys and 3 static methods.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类