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条)

相关推荐 更多相似问题