how to pass an array with N arguments to a bind_param inside a class?

1

I'm trying to develop a global class for paging

Follow the code below:

class Pager {
private $Sql;
private $Limit;
private $ArgType;
private $Page;
private $Arguments;
private $ArgNumber;
private $Connect;
private $result=array();
public function setSql($Sql){
    $this->Sql=$Sql;
    return false;
}
public function setLimit($Limit){
    $this->Limit=$Limit;
    return false;
}
public function setArguments(){
    $this->Arguments=func_get_args();
    $this->ArgNumber=func_num_args();
    return false;
}
public function setArgType($ArgType){
    $this->ArgType=$ArgType;
    return false;
}
public function setConnect($Connect){
    $this->Connect=$Connect;
    return false;
}
public function setPage($Page){
    $this->Page= isset($Page)?$Page:1;
    return false;
}
private function countRegister(){
    $Pager = $this->Connect->prepare($this->Sql);
    switch ($this->ArgNumber){
        case 1:
            $Pager->bind_param($this->ArgType,$this->Arguments[0]);
        break;
        case 2:
            $Pager->bind_param($this->ArgType,$this->Arguments[0],$this->Arguments[1]);
        break;
        case 3:
            $Pager->bind_param($this->ArgType,$this->Arguments[0],$this->Arguments[1],$this->Arguments[2]);
        break;
        case 4:
            $Pager->bind_param($this->ArgType,$this->Arguments[0],$this->Arguments[1],$this->Arguments[2],$this->Arguments[3]);
        break;
        case 5:
            $Pager->bind_param($this->ArgType,$this->Arguments[0],$this->Arguments[1],$this->Arguments[2],$this->Arguments[3],$this->Arguments[4]);
        break;
    }
    $Pager->execute();
    $Pager->store_result();
    return $Pager->num_rows;
    $Pager->close();
}
private function countPages($NumReg){
    return ceil($NumReg/$this->Limit);
}
private function prevPage(){
    if($this->Page>1)
        return $this->Page-1;
    else
        return false;
}
private function nextPage(){
    if($this->Page<$this->result['Pages'])
        return $this->Page+1;
    else
        return false;
}
private function getStart(){
    return ($this->Page-1)*$this->Limit;
}
public function execPager(){
    $this->result['Pages'] = $this->countPages($this->countRegister());
    $this->result['PrevPage'] = $this->prevPage();
    $this->result['NextPage'] = $this->nextPage();
    $this->result['Start'] = $this->getStart();
    $this->result['Limit'] = $this->Limit;
    return false;
}
public function getPager(){
    return $this->result;
}

}

But in%% I do not see a mode of doing private function execPage() with bind_param that I get in function array the only way was to use a case with predefined possibilities

Any suggestions on how to pass these parameters to the function and be able to do this?

I am using mysqli and the class is in the initial stage, in the function setArguments I will pass N arguments and put the type of argument by function setArgument being that it will be a setArgType then I will put inside string this way

$Pager->bind_param($this->setArgType, [Todos os N argumentos da setArguments]);
    
asked by anonymous 13.05.2014 / 19:34

1 answer

2

I will not go into the merits of object liability because that is not the purpose of the question but what you have implemented does not make sense.

Your class has no setter , public method so data can be set for the class to work. The closest thing to this would be getArguments () which implies that something will be returned and not set .

In addition to all the various changes you'll need to make to make this class plausible to use, it would look something like this:

class Paginador {

    // ...

    private $arguments = array();

    public function setArguments( $argument, $value ) {

        $this -> arguments[ $argument ] = $value;
    }

    public function getAruments() {
        return $this -> arguments;
    }

    // ...
}

Once your class has already been manipulated externally through the instance of the object.

Now, regarding the scope of the question.

First you have to define where this bind_param will come from. Is it from an outside class? From a native class?

Assuming your answer is PDO you should first prepare the statement , that is, the string SQL and yes, by setting the appropriate placehodlers , they are named (preceded by colon) or indexed (only queries) and in their execPage() method you iterate the arguments of your argument property and at each iteration you bind the new argument:

class Paginador {

    // ...

    private function execPage() {

        // Prepare statement

        foreach( $this -> arguments as $argument => $value ) {

            if( is_string( $value ) && ! is_numeric( $value ) ) {

                $dataType = PDO::PARAM_STR;

            } else {

                $dataType = PDO::PARAM_INT;
            }

            $stmt -> bindParam( $argument, $value, $dataType );
        }
    }

    // ...
}
  

The example above assumes that the statement prepared is in the $ stmt

Basically that's it, but you REALLY need to study Object Orientation better because simply throwing ideas into a class is not OOP.

One tip: Avoid func_get_args () whenever possible. This function should only be used when REALLY you do not know how many arguments will be informed for a given function / method and there is no other better way to do it.

In this scenario, the best way to do this would be to tell an array with key / value pairs or use Fluent Interfaces .

    
13.05.2014 / 20:09