Php类引用实例

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 carcreate 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.

I think you do not do real OOP but here, the only thing to do is call the functions you created. The only thing to do is migrate the three lines under your class definition to your template file. The logic behind is that you define your class once but you use it everytime you need it.

Then to reference a class in PHP it is like you do in C# but variables has to start with $. If you want to use a C# defined class in PHP you'll have to rewrite it in PHP because even if it should be possible to link such a C# code to a PHP one it will be such an overkill...

I'm not posting to have someone provide me the "this is the code you should type" generate a discussion on linking an OOP principle from C# to php ...

OOP, as a concept, is very similar across languages. It's the syntax that's different. I'm not a C# dev, but you can carry the vast majority of the OOP principles from C# over into PHP.

... do I need to break down this class to smaller classes each with a single function ...

In general, as long as the methods in your class are closely related to each other and the the job the class is designed to handle, no.

... or can I reference just certain outputs of a specific function?

You'd use your PHP class in the following manner:

$siteDirectories = new SiteDirectories();

You'd then call methods like so, and be able to "reference just certain outputs of a specific function".

$siteDirectories->arrayBuilder();

NOTE: I'd caution against the use of public properties in your classes, opting instead for accessors. The properties would then be private $sites; (or protected sites;), and you'd add a getSites() method to the class (same for $count).

EXTRA: PHP: The Right Way is an excellent reference for old and new PHP developers alike. I'd highly recommend you review it as you begin working with PHP.