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:
- Do I have to pass a reference to the printOutputclass 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?
- 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/>\n":"\n";
        }
    }
    function printObjectStatus($object, $html = true) {
        if ($debug) {
            echo '<pre>';
            echo $text;
            echo $html?"</pre><br/>\n":"</pre>\n";
        }
    }
}
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();
 
     
    