Background: I’m in the processes of learning C#, work dictates that I pick up php as well which stems this question.
I threw together a very simple example to illustrate a point. In C# I can reference a class, in this case private Car car
create an instance of that class, new Car();
and then proceed to use functionalities held within it, car.GasMilage();
.
Public abstract class vehicle
{
private Car car = new Car();
car.GasMilage();
}
Public class Car
{
private string color;
private int fuelCapacity;
private int milesDriven;
public double GasMilage()
{
double mpg = milesDriven/fuelCapacity;
return mpg;
}
}
I started creating the following php class which ultimately loops through a directory and populates my tabs for a website as well as the number of items within each tab. The number varies each week. What I would like to do is reuse functions within this classes so I guess my question becomes do I need to break down this class to smaller classes each with a single function or can I reference just certain outputs of a specific function? I have a couple books that discuss creating classes, functions, etc but I’ve not found good information on how all the parts fit together similar to the C# example. Thank you.
<?php
/*Establishes an array containing all the names of the centers*/
class SiteDirectories
{
/*Defines an array with a variable named Sites*/
public $Sites = array();
/*Variable for keeping count of patients within directories*/
public $count=0;
/*A foreach loop that loops through the contents of directory Dir, each directory it finds it assigns a temp variable Site*/
function arrayBuilder()
{
/*Defines a variable Dir a directory path*/
$Dir = "Photos/*";
/*Loops through each directory within the Photos directory, uses Site to hold the temp variable*/
foreach (glob($Dir) as $Site)
{
/*Takes the basename of the current directory and calls it SiteName*/
$SiteName = basename($Site)." (".$this->ptCount($Site).")";
/*Adds the SiteName to the array list Sites*/
array_push($this->Sites, $SiteName);
$this->count = 0;
}
}
function ptCount($Site)
{
foreach(glob($Site."/*") as $dir)
{
$this->count = $this->count + 1;
}
return $this->count;
}
function tabCreator()
{
$ct = 1;
foreach($this->Sites as $tab)
{
echo "<li><a href='#tab$ct'>".$tab."</a></li>";
$ct = $ct + 1;
}
}
}
$SiteNames = new SiteDirectories;
$SiteNames->arrayBuilder();
echo $SiteNames->tabCreator();
?>
It is referenced in my .php site as the following
<ul class='tabs'>
<?php
include 'SiteDirectories.php';
?>
</ul>
I'm not posting to have someone provide me the "this is the code you should type" rather generate a discussion on linking an OOP principle from C# to php as at present I've been unable to make the connect. Thank you.