0

I have made a separate class to connect to my database and that class is in a separate PHP file:

connect.php

class connect{

    function __construct(){
        // Connect to database
    }

    function query($q){
        // Executing query
    }
}
$connect = new connect();

Now, I made the object of the class $connect and when use it in a file like index.php it works:

index.php

require_once('connect.php');
$set = $connect->query("SELECT * FROM set");

Now, here it works fine, I don't have to recreate an object for the class and directly execute the query whereas in another file called header.php I have a class like this:

header.php

class header{

    function __construct(){
        require_once('connect.php');
        // Here the problem arises. I have to redeclare the object of the connection class
        // Without that, it throws an error: "undefined variable connect"
        $res = $connect->query("SELECT * FROM table");
    }

}

Why is it working in index.php and not in header.php?

Community
  • 1
  • 1
Keshav Nair
  • 423
  • 1
  • 14
  • 24
  • 2
    apart from being bad practise, it should work. The better approach would be to do `new header($connect)`, e.g. inject the dependency. – Gordon Feb 13 '12 at 09:22
  • `require_once('connect.php');` outside the class header and `global $connect;` in `__construct()` of header class.. – ahmet2106 Feb 13 '12 at 09:25
  • @ahmet2106 [forget `global` exists please](http://stackoverflow.com/questions/5166087/php-global-in-functions/5166527#5166527) – Gordon Feb 13 '12 at 09:26
  • but i would use `__construct(connect $connect)` in `class header` and define `$header = new header($connect);` instead in your main file. so only `require` connect.php and header.php once at top of your main file. – ahmet2106 Feb 13 '12 at 09:27
  • @Gordon i dont like global, and yeah, this is evil, i know ;) – ahmet2106 Feb 13 '12 at 09:28
  • @Gordon - Yes i was thinking the same that's why i ignore the word global – Keshav Nair Feb 13 '12 at 09:28
  • @ahmet2106 - i have multiple functions inside the header class so i have to add $connect as a parameter in every class?? – Keshav Nair Feb 13 '12 at 09:32
  • cannot reproduce: http://codepad.viper-7.com/He4kgu – Gordon Feb 13 '12 at 09:42
  • How many times are you including that file in whole your script? – Vyktor Feb 13 '12 at 09:53

1 Answers1

2

Your problem was probably in using require_once() instead of require(). When you included connect.php for the first time it worked well because variable(s) were initialized and class loaded, but when you tried later again require_once() prohibited repeated inclusion and therefore no variable was initialized.

Anyway, using include() inside constructor is... rarely justified. And including a file which will initialize local variables is bad idea too.

The proper code would look like:

<?php
    require_once('connect.php');
    require_once('header.php');

    $connect = new Connect();
    $header = new Header($connect);

And header.php:

<?php
    class Header{

        protected $connection = null;

        function __construct(Connect $connection){
            $this->connection = $connection;
            $res = $this->connection->query("SELECT * FROM table");
        }

    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • 1
    the first part does not apply here. include will import any class definitions into the global scope. OP is getting "undefined variable" error not "Class declarations may not be nested". The given alternative is fine though. – Gordon Feb 13 '12 at 09:35
  • @Gordon thank you for information, I've never done that so I never needed to solve such a problem. I've got to think how to incorporate it into my answer. – Vyktor Feb 13 '12 at 09:36
  • @Gordon This is the best edit I was able to come up with... If you have anything to add please feel free to extrend/edit my answer. – Vyktor Feb 13 '12 at 09:43
  • better now. apart from that i have no idea what the OP is doing wrong as I cannot reproduce the issue at all: http://codepad.viper-7.com/He4kgu – Gordon Feb 13 '12 at 09:46
  • doesnt make a difference for the testcase, does it? i'm not creating more than one Foo. – Gordon Feb 13 '12 at 09:50
  • @Gordon you're using `require_once` just once, but he's probably including it twice. (In case that OP = Original Poster) – Vyktor Feb 13 '12 at 09:53
  • bingo! that could be reason for the undefined property! good catch. – Gordon Feb 13 '12 at 10:08
  • @Gordon problem solved, I hope he'll change his codes to mentioned form. – Vyktor Feb 13 '12 at 10:11
  • you might want to add that to your answer though. comments often get overlooked. – Gordon Feb 13 '12 at 10:22
  • @Gordon it should be alright now, thank you for your help and afford. – Vyktor Feb 13 '12 at 10:30