dqf42223 2011-12-28 07:59
浏览 60
已采纳

php:如何在类中运行一个方法?

I started learning oop php and I don't understand how to make a method inside a class execute. This is the code:

class GrabData {
public $tables=array();
public $columns=array();
public $argList;


function __construct(){
    $this->argList=func_get_args();
    $pattern;
    var_dump($this->argList);
    if(!empty($this->argList)){

        foreach($this->argList as $value){
            if(preg_match("/.+_data/",$value,$matches)){
                if(!in_array($matches[0],$this->tables)){
                    array_push($this->tables,$matches[0]);
                    var_dump($this->tables);
                }
                $pattern="/". $matches[0] . "_" . "/";
                array_push($this->columns,preg_replace($pattern,"",$value));
                var_dump($this->columns);
            }

        }
    }
}

public function gen_query(){
    var_dump($this->argList);
    echo "haha";
}

    gen_query();
}

new GrabData("apt_data_aptname");

Now, the __construct function runs when I make a new GrabData object, but the gen_query function doesnt execute. How do I make it execute it ?

  • 写回答

3条回答 默认 最新

  • doureng1083 2011-12-28 08:22
    关注

    If you always want to have the gen_query function run when the class is initiated, you could link to it in the bottom of your constructor, like so:

    function __construct() {
        // Do your stuff here and then...
        $this->gen_query();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试