如何在不使用php中的函数的情况下将变量从一个类传递给另一个类?

时间:2015-06-23 14:58:29

标签: php function parameters

我有daoHistorial.php

 <?php

require "transferDao/daoTransferHistorial.php";

require "bdconnection.php";


    class daoHistorial{

            public function sqlSelect($tarea){

                    $aObjects=array();


                    $dbconn = new DBconnection();
                    $db = $dbconn->bdConnection();
                    $stmt = $db->prepare("SELECT t1.tar_id, t1.rea_seccion, t1.rea_porcentaje, t1.rea_descripcion, t1.hrs_tarea, t1.tar_fechtermino 
                                          FROM act_registtarea t1
                                          inner join act_tarea t2
                                          ON t1.tar_id = t2.tar_id 
                                          where t2.tar_nombre = '$tarea'");
                    $stmt->execute();
                    $stmt->setFetchMode(PDO::FETCH_ASSOC);
                    $result = $stmt->fetchAll();

                    foreach ($result as $row) {
                            $aTransfer = new daoTransferHistorial();
                            $aTransfer->setPorcentaje($row['rea_porcentaje']);
                            $aTransfer->setDescripcion($row['rea_descripcion']);
                            $aTransfer->setFechaTermino($row['tar_fechtermino']);
                            $aTransfer->setSeccion($row['rea_seccion']);
                            $aTransfer->setHora($row['hrs_tarea']);

                            $aObjects[]=$aTransfer;
                    }

                    print_r($aObjects);
                    return $aObjects;
            }   
    }
?>

我有controllerHistorial.php

<?php

require "dao/daoHistorial.php";  

class HistorialTareas {

    public function getHistorial() {

        $aTransfer = new daoHistorial();

        foreach($aTransfer->sqlSelect() as $sKey=>$oValue){    
            $list[] = array('porcentaje' => $oValue->getPorcentaje(),
                            'descripcion' => $oValue->getDescripcion(),
                            'fecha_termino' => $oValue->getFechaTermino(),
                            'seccion' =>$oValue->getSeccion(),
                            'hora' =>$oValue->getHora()
                            );
        }
        print_r($list);
        return $list;
    }
}

?>

这里:foreach($aTransfer->sqlSelect() as $sKey=>$oValue){我有一个问题是

缺少参数
  

的SQLSelect()

我需要使用`daoHistorial.php中的sqlSelect($tarea)

我知道我需要传递public function getHistorial($tarea)

中的参数

但我需要这个:

public function getHistorial() {
$aTransfer = new daoHistorial();
foreach($aTransfer->sqlSelect($tarea) as $sKey=>$oValue){

因为在我需要getHistorial()之后没有参数。

1 个答案:

答案 0 :(得分:1)

将相关文件更新为以下内容:

daoHistorial.php

<?php
require "transferDao/daoTransferHistorial.php";
require "bdconnection.php";

class daoHistorial{
    public $tarea = NULL;

    public function setTarea($val) {
        $this->tarea = $val;
    }

    public function sqlSelect() {
        $aObjects=array();

        $dbconn = new DBconnection();
        $db = $dbconn->bdConnection();

        $incStatement = ($this->tarea != NULL) ? "where t2.tar_nombre = '.$this->tarea.'":"";

        $stmt = $db->prepare("SELECT t1.tar_id, t1.rea_seccion, t1.rea_porcentaje, t1.rea_descripcion, t1.hrs_tarea, t1.tar_fechtermino 
                                          FROM act_registtarea t1
                                          inner join act_tarea t2
                                          ON t1.tar_id = t2.tar_id 
                                          ".$incStatement);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $result = $stmt->fetchAll();

        foreach ($result as $row) {
                            $aTransfer = new daoTransferHistorial();
                            $aTransfer->setPorcentaje($row['rea_porcentaje']);
                            $aTransfer->setDescripcion($row['rea_descripcion']);
                            $aTransfer->setFechaTermino($row['tar_fechtermino']);
                            $aTransfer->setSeccion($row['rea_seccion']);
                            $aTransfer->setHora($row['hrs_tarea']);

                            $aObjects[]=$aTransfer;
         }

         print_r($aObjects);
         return $aObjects;
    }   
}
?>

controllerHistorial.php

<?php
require "dao/daoHistorial.php";  

class HistorialTareas extends daoHistorial {

    public function getHistorial() {

        foreach($this->sqlSelect() as $sKey=>$oValue){    
            $list[] = array('porcentaje' => $oValue->getPorcentaje(),
                            'descripcion' => $oValue->getDescripcion(),
                            'fecha_termino' => $oValue->getFechaTermino(),
                            'seccion' =>$oValue->getSeccion(),
                            'hora' =>$oValue->getHora()
                            );
        }
        print_r($list);
        return $list;
    }
}
?>

现在访问HistorialTareas时,你可以这样做

<?php
$historial = new HistorialTareas;
$getData = $historial->getHistorial();

表示正常结果,或过滤

<?php
$historial = new HistorialTareas;
$historial->setTarea('VALUE HERE');
$getData = $historial->getHistorial();