• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[PHP] Simple Content Display class

Zonet

Web Developer
Joined
Sep 1, 2008
Messages
4,393
Reaction score
52
Location
Tibia VS RL-life, guess whos back?
Hey, here is my simple content display!

PHP:
    <?PHP
            class View
            {
            public $_show = null;
                    public function __construct( $subtopic, $_404 = 'not_found' )
                    {
                            if( file_exists( './pages/'.$subtopic.'.php' ) && isset( $subtopic ) )
                            {
                                            $this->_show = './pages/'.$subtopic.'.php';
                            }
                            else
                            {
                                    header( 'Location: ?subtopic='.$_404 );
                            }                      
                    }
                   
                    public function display() {
                        require_once( $this->_show );
                    }
            }
    ?>

Usage:
PHP:
<?PHP
            echo $view->display();  
?>

etc ..

What do you think, is it a good way?
 
always hated classes and OOP, but ok. just feels like bloat
 
OOP is great for somewhat larger projects (smaller ones as well in my opinion, but I can see how someone may dislike it (especially if that person have not worked with it before (or at least not much (many parenthesis!)))).

As for your content displayer, you should make it a habit of always adding the underscore to private/protected variables, not public ones (same goes for methods) as this is the most common and widely known use of it.

The isset( $subtopic ) within the if statement is also unnecessary as $subtopic always has to have a value (whether it be NULL or not, it still has to be defined).

What I would also do is change;
PHP:
if( file_exists( './pages/'.$subtopic.'.php' ) && isset( $subtopic ) )
{
     $this->_show = './pages/'.$subtopic.'.php';
}
else
{
     header( 'Location: ?subtopic='.$_404 );
}
to something like;
PHP:
if( !file_exists( './pages/'.$subtopic.'.php' ) )
{
     header( 'Location: ?subtopic='.$_404 );
     exit;
}

$this->_show = './pages/'.$subtopic.'.php';
Other than that, it looks good!
 
Last edited:
Back
Top