加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 15:56:35 所属栏目:PHP教程 来源:网络整理
导读:我有daoHistorial.php: ?phprequire "transferDao/daoTransferHistorial.php";require "bdconnection.php"; class daoHistorial{ public function sqlSelect($tarea){ $aObjects=array(); $dbconn = new DBconnection(); $db = $dbconn-bdConnection(); $st
我有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).

我知道我需要在公共函数getHistorial($tarea)中传递参数

但我需要这个:

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

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

解决方法

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

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.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(),'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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读