I've looked all over and ive not found a good answer, is there a way you can take your functions and make them in seprate files and call them to the class? it seems no one has tried this or i am ooking in the wrong place

Recommended Answers

You want to put parts of a class in a separate file? Yes, that's possible (using include), but I don't recommend it (bad design). Better yet, give a code example explaining what part you want to move to a separate file.

Jump to Post

yes you do that using include function of php put what you want in your file and include that file in appropriate location or another file for reference look at this sites
http://php.net/manual/en/function.include.php

Jump to Post

All 7 Replies

You want to put parts of a class in a separate file? Yes, that's possible (using include), but I don't recommend it (bad design). Better yet, give a code example explaining what part you want to move to a separate file.

_class.php

<?php 


    class ModernCMS{

    include'vars.php';
    include'functon_connect.php';


        //Basic Functions For Getting Content From Database

        function get_content($id = ''){


                if($id != ""):
                    $idv = mysql_real_escape_string($id);
                    $sql = "SELECT * FROM cms_content WHERE id = '$id'";

                    $return = '<p><a href="index.php">Go Back To Content!</a></p>';
                else:
                    $sql = "SELECT * FROM cms_content ORDER BY id DESC";
                endif;

                $res = mysql_query($sql) or die(mysql_error());

                if (mysql_num_rows($res) != 0):         
                    while ($row = mysql_fetch_assoc($res)) {
                        echo '<h1><a href="index.php?id=' . $row['id'] . '">' . $row['title'] . '</a></h1>';
                        echo '<p>' . $row['body'] . '</p>';
                    }
                else:
                    echo '<p>Uh Oh! This dosen\'t exist!</p>';
                endif;

                echo $return;
        }   




    }// Ends Our Class

?>

Vars.php

        var $host ;
        var $username ;
        var $password ;
        var $db ;

basicly im trying not to get a long file to find one thing that im lookinig for.

im trying not to get a long file

That shouldn't matter. The class has a purpose, and chopping it into pieces doesn't make it easier to maintain. Consider a book where every page is on a different shelve.

Member Avatar

Including files like this inside your classes can be a bit problematic, as if the file is moved, you then have to rewrite all your classes that include that file. Maybe a "better" way would be to create a DB class and pass that via dependency injection. Your class then doesn't have to worry about creating a connection, selecting as db, etc, it can just get on with the one job - i.e. in this case, returning specific data.

I'm assuming that your class will have more methods.

 class ModernCMS{

    private $db;

    public function __construct($db)
    {
        $this->db = $db;
    }

    public function getContent($id=NULL)
    {
        if($id && is_int($id))
        {
            $this->db->query("...");
            //etc
        }
    }
 }       

You can use $db object as if you'd just created it inside the class itself. It shouldn't matter where the db object comes from as long as it shares a common interface for methods like 'query', 'execute' or whatever you decide.
I suggest PDO as opposed to the defunct mysql_* functions. Just a thought.

Pretty much what is there is about it, might have a few of them duplicate or change them to look for arguments

Member Avatar

Pretty much what is there is about it, might have a few of them duplicate or change them to look for arguments

Sorry, but I don't understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.