Jump to content

variables within classes


ameliabob

Recommended Posts

I am trying to create a class which will concatenate variables but it appears as though each time a function within a class is called the variables are reinitiated. Is there a way that the variables can be held so as to allow them to be added to?

 

The first code box is the calling functions and the second is the class.

 

 

<?php
include ("myClasses.php");

    $sTable = new TABLE();
    $sTable->setHeading("This is the heading");
    $sTable->setHeadingItem("Symbol");
    $sTable->setHeadingItem("Name");
    $sTable->setDetailItem("F");
    $sTable->setDetailItem("Ford");
    $sTable->endTable();
    echo($sTable->showTable());
?>

 

myClasses.php

 

<?php
class TABLE {

private  $border;
private  $hdrStarted = false;
private  $rowStarted = false;
private  $numberOfRows=0;
private  $s;

    function __construct($border=null){
        $s = "<table ";
        if(is_null($border))
            $s .= ">";
        else    
            $s .= "border='".$border."'>";
    }
    function setHeading($t){
        $s .= "<caption>".$t."</caption>";
        echo $s;
    }
    function setHeadingItem($t){
        if(!$hdrStarted){
            $s .= "<tr>";
            $hdrStarted = true;
        }
        $s .= "<th>".$t."</th>";
    }
    function setDetailItem($t){
        if($hdrStarted){
            $s .= "</tr>";
            $hdrStarted = false;
        }
            
        if(!$rowStarted){
            $rowStarted = true;
            $s .= "<tr>";
        }
        $s .= "<td>".$t."</td>";
    }
    function endTable(){
        if($rowStarted || $hdrStarted)
            $s .= "</tr>";
        $s .="</table>";
    }
    function showTable(){
        return $s;
    }
}
?>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...