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?

9
  • 2
    apart from being bad practise, it should work. The better approach would be to do new header($connect), e.g. inject the dependency. Commented Feb 13, 2012 at 9:22
  • require_once('connect.php'); outside the class header and global $connect; in __construct() of header class.. Commented Feb 13, 2012 at 9:25
  • @ahmet2106 forget global exists please Commented Feb 13, 2012 at 9: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. Commented Feb 13, 2012 at 9:27
  • @Gordon i dont like global, and yeah, this is evil, i know ;) Commented Feb 13, 2012 at 9:28

1 Answer 1

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");
        }

    }
Sign up to request clarification or add additional context in comments.

10 Comments

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 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.
@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.
better now. apart from that i have no idea what the OP is doing wrong as I cannot reproduce the issue at all: codepad.viper-7.com/He4kgu
doesnt make a difference for the testcase, does it? i'm not creating more than one Foo.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.