duandang6352 2013-12-26 16:30
浏览 36
已采纳

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.

  • 写回答

2条回答 默认 最新

  • dtf54486 2013-12-26 16:48
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘