SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Namespace assistance

    Hi all, i have been a fan of sitepoint for a while now.

    I have come to a hard place and require a little assistance.

    I have the following Class (file : headScript.php)
    PHP Code:
     <?php
        
    class headScript
        
    {
        public function 
    connection()
        {
        try
        {
        
    $dbh = new PDO("mysql:host=localhost;dbname=xxx"'xxx''xxx'); // Dev
        
    return $dbh;
        }
        catch(
    PDOException $e)
        {
        echo 
    "Error :- " $e->getMessage();
        die();
        }
        }
        public function 
    headLoad()
        {
        
    $cxn $this->connection()->prepare("SELECT scriptCode FROM head_scripts WHERE active != '0'");
        
    $cxn->execute();
        
    $cxn->setFetchMode(PDO::FETCH_ASSOC);
        while(
    $row $cxn->fetch())
        {
        print 
    "<script>";
        print 
    $row['scriptCode'];
        print 
    "</script>";
        }
        }
        }
        
    ?>
    I'd like to remove the connection function and put this in its own file, then using namespace to pull this in. That way i have one file which can be called via namespace and i can use this function.

    Am I thinking this is possible when it is not?

    I have tried a few things but cant seem to get it to work.

    Can anyone offer assistance?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Don't see why you need namespacing...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Don't see why you need namespacing...
    logic_earth thank you for posting back.

    My reason is so that i have the db connection accessable for where i need it. I do not wish to keep typing the connection info.

    Hope that makes sense?

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You don't need namespacing for that.
    Just throw the connection into a file then include it. Then for methods/functions that need it pass it as a parameter/argument.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    You don't need namespacing for that.
    Just throw the connection into a file then include it. Then for methods/functions that need it pass it as a parameter/argument.
    Thank you. had thought of that.

    What is the use/need of namespacing then, or have i missed the point on them?

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Namespacing is for much larger projects...which have multiples of interconnecting libraries.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,244
    Mentioned
    155 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Namespacing is for much larger projects...which have multiples of interconnecting libraries.
    That is one purpose, another is when you need to separate components that are likely to have similar names for different purposes. Example, I might have an Output file for an Email form and an object that stores Email related data and handles sending emails. I could name them OutputEmail and ObjectEmail (yes, you could have an output function in your email object, but I like to separate content from business logic/data access -- my .NET side is showing again), or I could use namespaces and have \Output\Email and \Object\Email. Namespaces are a way to organize your classes and libraries.

    @Wbsite Creation, you likely want to create a Database Class. One that can help assist your database querying, connections, fetching results, etc. You would typically write this in a separate file and use require_once to include the class and then utilize the class where needed.


Bookmarks

Posting Permissions

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