-1

I have One utility Class Called emailUtility.php as follows:

<?php
class emailUtility
{
    public function SendMail($subject,$body)
    {               

    }

}
?>

I have two more class, where I want to use this SendMail Method of the emailUtility class. I have first class (Test1.php) that uses the emailUtility as below.

<?php
include("emailUtility.php")
class Test1
{
    $emailUtil=new emailUtility();
    $emailUtil->SendMail("","");
}

?>

Now, when i tried to use the same emailUtility class in another class on the same way, it is not working. But, when i remove the use of emailUtility from Test1, then Test2 is working fine. Not working means when browsing, no HTML is generated for other part of the code in same file as well.

<?php
include("emailUtility.php")
class Test2
{
    $emailUtil=new emailUtility();
    $emailUtil->SendMail("","");
}

?>

How can i use one class in multiple other classes? Also, is it so that one class can be included only once in php?

Yauvaraj Rimal
  • 620
  • 5
  • 22

2 Answers2

0

From what I understand, you are probably including the emailUtility.php file multiple times and it causes your program to crash since the classes is being overwritten. Just replace the line below

include("emailUtility.php");

with

include_once("emailUtility.php");
Yannick Y
  • 2,778
  • 1
  • 16
  • 22
0

Two reasons why it clashes:

  1. Classes cannot be redeclared in PHP. By using include, you included the script twice, and so the classes get declared twice, hence fatal error.

  2. You forgot to declare methods/variables in class names. Syntax error.

Try turning on error reporting to see the error in action instead of white pages. Include this line in the very early of your application: error_reporting(E_ALL);. Check doc here.

Solution

  1. Change include to include_once instead.

    Differences:

    • include will include a script, and show warning if script not found
    • include_once will include a script strictly once, and show warning if script not found
    • require will include a script, and throw fatal error when the script not found
    • require_once will include a script once, and throw fatal error when the script not found


2. Declare your Test1.php as follow:

Test1.php

<?php
include_once("emailUtility.php");
class Test1 {
    /**
     * @var emailUtility
     */
    public $emailUtility;

    public function __construct() {

        //Note that we usually define class names as CamelCase "EmailUtility"
        //so it will be much more readable. For this case, ideal case should be
        //$this->emailUtility = new EmailUtility();

        $this->emailUtility = new emailUtility();
    }

    /**
     * My send email
     */
    public function mySendEmail() {
        $this->emailUtility->SendMail();
    }
}

And Test2.php should follow the same declaration.

One thing to take note, it's always a good practice to define class names as CamelCase, for example, EmailUtility. It made the code more readable.

Community
  • 1
  • 1
Lionel Chan
  • 7,894
  • 5
  • 40
  • 69