accessing class variables in PHP

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ckb
Forum Newbie
Posts: 5
Joined: Sun Aug 10, 2003 1:03 am

accessing class variables in PHP

Post by ckb »

hi all.
one doubt/....

now i am using class in PHP for my pgms...

my class is

GetAll.inc.php



<?
class GetAll{
function GetRegion($num)
{
$query=mysql_query("select reg_id ,reg_name from cregion ORDER BY reg_name");

echo"<select size=\"1\" name=\"RN\">";
echo "<option >Choose a Region</option>";

while($row = mysql_fetch_array($query))
{
$reg=$row[reg_id];

echo "<option value=\"$reg\"";
if ($reg == $num)
echo " selected";
echo ">$row[reg_name]</option>n";
}
echo "</select>";
}
}
?>

****************************************

i am making use of this class in another php file like this and i am getting result also...(ie. for populating dropdown box Region)

that part of code is this:

<tr>
<td width="158" height="22" align="right" class="officeBearers">Region</td>
<td width="5" height="22">&nbsp;</td>
<td width="532" height="22">
<?php

include("connect.inc.php");
$objmysqlcon= new Connect();
$objmysqlcon->start_connectmysql();
include("GetAll.inc.php");
$post[0]=$_POST['RN'];
$O=new GetAll();
$O->GetRegion($post[0]);
?>
</td>
</tr>

Now i want to save a combination for making use for search later .....

like

$search_strval = Trim($vREGID).Trim($vESTNAME);
$search_strval .= Trim($vCOMPUTERIZED_YR);


$qrysearch_str= "update hos_computer SET hos_computer.search_str = '$search_strval' where ESTCD ='$vESTCD'";
$searchresult_str=mysql_query($qrysearch_str)
or die("Error in query:$qrysearch_str.". mysql_error());




here

$vREGID=$_POST['RN']; which is from that class file.

i need to get the region name in place of this and form my serach_strval like

$search_strval = Trim($vREGIONNAME).Trim($vESTNAME);
$search_strval .= Trim($vCOMPUTERIZED_YR);



HOW TO GET THIS REGION NAME FROM THAT CLASS.....IN MY DROP DOWN PASSING VALUE IS REGIONID AND DISPLAYING VALUES IS REGIONNAME...SO I WANT THIS DISPLAYING VALUE TO BE IN SEARCH STRING....


pls help me
thanx
ckb
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Producing a simplified example..

Code: Select all

<?php
    class temp
    {
        var $region;  // The class variable to use.

        function temp($value)
        {
            $this->setRegion($value);
        }

        function getRegion()
        {
             return $this->region;
        }

       function setRegion($region)
       {
            $this->region=$region;
       }
    }

    // Main block creates the Object and assigns the $region variable within it.
    $O=new $temp("Europe");

    ...

    // We need to get the value...
    $useval=$O->getRegion();
?>
$useval=$O->getRegion() could be replaced by $o->$region without a method call but this
is considered bad practice. 8O

Also you should name the class constructor,normally the first function the same as the class name :wink:.

(When I say "method" this is Object Oriented speak for the PHP functions within a class.)

Hope that helps
Post Reply