doushi9780 2013-12-17 22:25
浏览 37

如何在其他类之间使用PHP类[重复]

This question already has an answer here:

I know the basics of PHP and have only ever used it to debug WordPress code generally, but now I want to write my own little program to download an email and process an attachment and I have decided to try using classes as I have a basic understanding of OO programming.

SO PLEASE READ: I am a novice! I don't know what on earth dependency injection is or means...

My issue is that I have created a function called printStatus(), so I can toggle on/off output of comments. I was looking at this, and I'm not sure how or if it would fit into a class structure or if I need to include this function in every other class I create?

Basically - If I created a class, I would need to make it available to all other classes (i.e. a global class) but I'm not sure if that is achievable.

My questions are:

  1. Do I have to pass a reference to the printOutput class to and from every class I use to have it available to me OR can I declare it globally to make it available to all classes OR do I need to include the same function in every class I create?
  2. I've created a MySQL Connection class and I am passing that into each object for use - should (can I) declare it globally and just make it available to all classes?

Thanks for the 101.

Here is my code, am I going down the right path?: (see specifically, references to printStatus())

PS - $formatoutput->printStatus() does not work within other classes - I'm looking to understand what structure is required to make it work.

class.formatoutput.php:

class formatOutput {

    var $debug = true;

    function printStatus($text, $html = true) {

        if ($debug) {
            echo $text;
            echo $html?"<br/>
":"
";
        }
    }

    function printObjectStatus($object, $html = true) {

        if ($debug) {
            echo '<pre>';
            echo $text;
            echo $html?"</pre><br/>
":"</pre>
";
        }
    }
}

class.connection.php:

class Connection
{
    var $db_host = "host";
    var $db_name = "name";
    var $db_user = "user";
    var $db_pass = "pass";
    var $db_conn;

    function connectToDatabase() {

        $db_conn = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);

        if (!$db_conn) {
            die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
        }
        else
        {
            $this->db_conn = $db_conn;
            $formatoutput->printStatus( "Connection established");
        }
        return $this->db_conn;

    }

    function closeConnection() {
        mysqli_close($this->db_conn);
        $formatoutput->printStatus( "Connection closed");
    }
}

class.customer.php:

class Customer {

    var $customer_id;
    var $customer_name;

    function getCustomer($connection, $user_id) {

        $query = "SELECT id, name FROM customer WHERE user_id=$user_id";

        $result = mysqli_query($connection, $query);

        if($result === FALSE) {
            die('Connect Error (' . mysqli_errno() . ') ' . mysqli_error());
        }

        $row_count = mysqli_field_count($connection);
        $formatoutput->printStatus( "COUNT: (".$count.")");
    }
}

index.php:

include 'class.formatoutput.php';
include 'class.connection.php';
include 'class.customer.php';

$formatoutput = new formatOutput();
$formatoutput->printStatus('Start new Connection()');
$connection = new Connection();
$connection->connectToDatabase();
$customer = new Customer();
$customer->getCustomer($connection->db_conn, "1");
$connection->closeConnection();
</div>
  • 写回答

1条回答 默认 最新

  • dragon87836215 2013-12-17 22:37
    关注

    Declare the function printStatus as static:

     static function printStatus($text, $html = true)
    

    You can call this function by using "::",

     formatOutput::printStatus("hello");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程