www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Call to undefined function Blog\DB\connect?

  1. #1
    Join Date
    Apr 2014
    Posts
    19

    Call to undefined function Blog\DB\connect?

    Hi,

    This is driving me crazy! Here is my code:

    PHP Code:
    <?php

    require '../blog.php';

    view('admin/create');
    This file is called index.php which is in a directory called admin.

    Here is the code for another file called blog.php which is the same directory as admin.

    PHP Code:
    <?php

    require 'functions.php';
    require 
    'db.php';

    $conn Blog\DB\connect($config);

    if (!
    $conn) die('Could not connect');
    Now, when I navigate to admin/index.php I get an error:

    Fatal error: Call to undefined function Blog\DB\connect() in C:\xampp\htdocs\sandbox\blog\blog.php on line 6.

    I tried putting namespace Blog\DB and deleting Blog\ from the blog.php file but I keep getting that error.

    I don't know what the problem is, other pages that use blog.php are working fine.

    Thanks

  2. #2
    Join Date
    Mar 2014
    Location
    Corpus Christi, TX
    Posts
    88
    Try changing "require" to "include"

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    What version of PHP are you running this under? (Anything earlier than 5.3.0, and PHP won't understand the namespace notation [or concept] being used in your code.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2014
    Posts
    19
    nah now even the other pages are not working

  5. #5
    Join Date
    Apr 2014
    Posts
    19
    5.5.9

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Yeah, I'd leave those require()'s as they are: you want an error if it can't find an include file.

    Should there be a leading "\":
    PHP Code:
    $conn = \Blog\DB\connect($config); 
    ?

    I've not really used namespaces yet in PHP, so may not be a whole lot of help. Do any of the files in question actually have a "namespace" declaration anywhere (normally before any other PHP code)?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    The problem is the various depths of directories and PHP's internal directory pointer.
    When opening blog/admin/ all relative URIs (i.e. links) point to that directory.
    You can prevent this by using absolute URIs (e.g. /xampp/htdocs/sandbox/blog/).
    In situations where you cannot use absolute URIs (i.e. production application). You can do something along these lines:
    Place this at the top of each page (do not add to files that are included) change the value of $root to resolve to the root directory of the script.
    PHP Code:
    // path resolution
    $root './'
    for pages in blog/<dir>.
    PHP Code:
    // path resolution
    $root '../'
    Then alter all the include() and require() calls to use this variable.
    PHP Code:
    include $root 'inc/functions.php'
    Hope this helps. Sorry for lack of explanation of POSIX directory pointers.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    I suspect it's not an include problem, as require() will throw a fatal error that's pretty obvious if it can't find the specified file. I'm more inclined to think it's a namespace issue.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Apr 2014
    Posts
    19
    Quote Originally Posted by NogDog View Post
    Do any of the files in question actually have a "namespace" declaration anywhere (normally before any other PHP code)?
    yh, the db.php file has namespace Blog\DB on the top line before any code.

  10. #10
    Join Date
    Apr 2014
    Posts
    19
    this is driving me nuts, I looked at the source code of the tutorial I'm following, and I have the exact same code!

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,384
    Is there a link to the tutorial you could give us?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Apr 2014
    Posts
    19
    Quote Originally Posted by NogDog View Post
    Is there a link to the tutorial you could give us?
    It's a premium tutorial, but I downloaded the source code, sou you can check it.

    https://onedrive.live.com/?gologin=1...44C62132%21130

    blog 1 folder is the tutorials source code, blog is mine

  13. #13
    Join Date
    Apr 2014
    Posts
    19
    Interesting..

    Even when I remove the namespace declaration and notations, the other pages continue to work fine except admin/index.php, except this time the fatal error says undefined function connect() and not Blog/DB/connect().

    So the problem seems to be around admin/index.php not recognizing the statement: require '../blog.php'.

  14. #14
    Join Date
    Apr 2014
    Posts
    19
    Aha, I have moved the index.php file, that is in the admin folder, one directory up, so that now it is in the same directory as blog.php (and renamed it index2.php because there is already an index.php file), and changed the require '../blog.php' statement to require 'blog.php' and it works!

    So the problem is definately something to go with the statement require '../blog.php' because when the file is in its own directory it does not work but when it is in the same directory as blog.php it works!:S

  15. #15
    Join Date
    Apr 2014
    Posts
    19
    Quote Originally Posted by NogDog View Post
    Yeah, I'd leave those require()'s as they are: you want an error if it can't find an include file.

    Should there be a leading "\":
    That was just a typo I made, its not on the file. Like I said before, all the other pages indeed working and they require blog.php so the rpoblem is definately not the blog.php file but the index.php that is in the admin folder.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles