0

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/>\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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
php-b-grader
  • 3,191
  • 11
  • 42
  • 53
  • Actually that approach is called [factory design pattern](http://www.ibm.com/developerworks/library/os-php-designptrns/) – amarjit singh Dec 17 '13 at 22:41
  • OOP rule #1: avoid global state – Gordon Dec 17 '13 at 22:41
  • @amarjitsngh one could use a Factory (or Builder) here to assemble the object graph when it gets more complex. But what the OP really needs is just simple plain old dependency injection. – Gordon Dec 17 '13 at 22:45
  • What the OP needs is a simple explanation as to how classes can and cannot be accessed... – php-b-grader Dec 17 '13 at 23:57

1 Answers1

1

Declare the function printStatus as static:

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

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

 formatOutput::printStatus("hello");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zane6888
  • 36
  • 3
  • this is the equivalent of just using a regular procedural function. The question is tagged OOP, so you want to avoid a solution leading to tight coupling and hard to test code. – Gordon Dec 17 '13 at 22:40