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

报告相同问题?