How to use php classes between other classes

First time user - I’m sick of stack exchange - hopefully the forum contributors here treat novices like me which much less disdain!

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 have a slim grasp of OOP

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 Question’s 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:
[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"; 
		}	
	}
}

[/php]

class.connection.php:
[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");
    }

}

[/php]

class.customer.php:
[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.")");

	}

}

[/php]

index.php:

[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();

[/php]

There are some problems here, let’s start with something small/easy and expand later on.

[php]<?php
class formatOutput {

var $debug = true;

function printStatus($text, $html = true) {
if ($debug) {
echo $text;
echo $html ? “
\n” : “\n”;
}
}

function printObjectStatus($object, $html = true) {
if ($debug) {
echo ‘

’;
echo $text;
echo $html ? “

\n” : “\n”;
}
}
}[/php]

When using parameters/functions in an object you must use scope. Accessing a function from another function is done with
[php]$this->otherFunction();[/php]

The same with variables ($this->someVar).

[hr]

Also the text variable in the second function is not set anywhere, so it will fail.

[hr]

lastly I just changed the first variable declaration. You don’t need to include var, you can however benefit from mentioning scope access (public, private, protected)

[php]<?php
class formatOutput {

private $debug = true;

function printStatus($text, $html = true) {
if ($this->debug) {
echo $text;
echo $html ? “
\n” : “\n”;
}
}

function printObjectStatus($object, $text, $html = true) {
if ($this->debug) {
echo ‘

’;
echo $text;
echo $html ? “

\n” : “\n”;
}
}
}[/php]

I would recommend you to use an IDE (more info in my signature), not sure what you use today, but your editor should show you errors on the stuff I’ve mentioned above.

That’s all I have time for atm :\ Please try to do some changes, or ask some questions about how this work and I (or someone else) will get back to you :slight_smile:

I will expand slightly at the moment on JimL’s post. Your formatOutput class should acutally be in a configuration file that is the included in your index.php. You should/ could I guess is a better way of saying it, have something set whether $debug should be true or not, ie:

[php]$host = substr($_SERVER[‘HTTP_HOST’], 0, 5);

if (in_array($host, array(‘local’ ‘127.0’, 192.1’))) {
$debug = true;
} else {
$debug = false;
}[/php]

Then have printStatus declared as public and you can use it in your index.php file after you create a new instance.

Sponsor our Newsletter | Privacy Policy | Terms of Service