How to extending a class in another file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

How to extending a class in another file

Post by Axllrod »

So I have been doing some OOP and I hit a snag. I'm trying to extend a class in another php file. The extended classes should load dynamically. I got it set up like this:

-->bootstrap.php file

Code: Select all

<?php
class bootstrap{
    
    public $lang;
    public $page;
    public $action;
    public $id;
    public $message;

    public function __construct() {
       
    }
        
        public function buildPage() {
           
            if($this->page == 'home'){
                require_once CONTR . 'view.php';
                new view($this->lang, $this->page, $this->action, $this->id, $this->message);
            }
        }
    }
    
}
--> and the view.php file

Code: Select all

class view extends bootstrap{
    
    public $lang;
    public $page;
    public $action;
    public $id;
    public $message;

    public function __construct($lang, $page, $action, $id, $message) {
        parent::__construct();
        $this->lang = $lang;
        $this->page = $page;
        $this->action = $action; 
        $this->id = $id; 
        $this->message = $message;
        $this->showPage();
    }


    
    public function showPage() {
 
            require_once VIEW . 'header.php';

            //echo VIEW . 'header.php';
            
            //prepare SQL statements
            $page = $this->page;
            $name = "name_" . $this->lang;

            //SQL statement
            $selectdata = mysql_query("SELECT * FROM pages WHERE $name = '$page'");
            $result = mysql_fetch_array($selectdata);

            //Echo content
            if($result["content_$this->lang"] == NULL){
                echo "Er is nog geen content hier";
            } else {
                echo $result["content_$this->lang"];
            }


            //Debugging
            echo '<br>' . $this->lang;
            echo '<br>' . $this->page;
            echo '<br>' . $name;
            echo '<br>' . $page;
            

            require_once VIEW . 'footer.php';
            
        }
}
I have been trying lots of different things but I think I am quite close. Does anyone have an idea how to solve this one?
The showpage function does actually work when I put it in the bootstrap class, but when I put it in the view class, nothing works.
Thanks in advance!
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How to extending a class in another file

Post by mecha_godzilla »

Hi,

Do you have error messages switched on in PHP? That is usually a good way to see what's going on if your classes aren't working as expected.

A couple of points though:

1. I don't think you can instantiate your "view" class inside your "bootstrap" class, given that "view" is the child of "bootstrap". When I've created page object classes before, what I've done is put some basic URL routing code in my "index.php" script and instantiated my page object in there - my page classes all extend a base class that contains the functionality common to each page object, and I use class autoloading to include the scripts for that particular page class and the base class. I then call a routine in the base class that authenticates the user, generates the menus and *only* after these functions have been completed does it then call the method defined in every page object to carry out the page-specific functions. So, in essence, the way you're trying to organise your classes is correct but the implementation isn't quite right - keep in mind that your "bootstrap" class is only relevant in the context of your "view" class (and therefore there would never be any occasion on which you would want to instantiate the "bootstrap" class directly) and make sure you understand the order in which your object "events" need to happen - for example, the "bootstrap" class can't build the page unless the "view" class has already told it what page template to load.

2. You don't need to redeclare any properties in your "view" class that are already declared in your "bootstrap" class, but you can obviously add new ones if you want to.

HTH,

Mecha Godzilla
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: How to extending a class in another file

Post by Axllrod »

That's a lot of information. A lot of very good information. I'm going to try to implement and rewrite my code a bit.
Thanks for the advice!
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How to extending a class in another file

Post by mecha_godzilla »

No problem :mrgreen: Don't try to solve all your problems at once though - just get the OOP relationship working correctly first and then start adding in all the functionality afterwards. I think OOP is one of those concepts that if you don't grasp immediately (as I didn't, but maybe you will) you just need to write some *really* basic standalone scripts to demonstrate to yourself things like inheritance, class extension, access to properties, etc. That doesn't mean that you necessarily have to understand everything about OOP though before you can make good progress and start writing applications - I've never found a practical use for interfaces in my own code, for example.

If you need any specific help with your code, just say so and myself and the other forum users will be happy to try and assist.

M_G
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: How to extending a class in another file

Post by Axllrod »

I tried to change my code according as I thought it should happen. However, it didn't do a lot :D Like you suggested, I started with some basics. With inheritance and such. If I run into some trouble, I'll post my questions here. Thanks a lot for the advice and help so far!!
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: How to extending a class in another file

Post by Axllrod »

So I have been playing arround a bit with the information you gave me and suggested and surprisingly I got it to work. The problem is, I don't really know how :lol:
If somemone could explain to me how the php code does his thing, it would be greatly appreciated.
My folder setup is as following:
root/
.htacces
index.php
--->libs/
--->bootstrap.php
--->help.php
--->index.php

The files look like this:
->index.php

Code: Select all

<?php

function __autoload($class) {
    include 'libs/' . $class . '.php';
}


$site = new bootstrap();
$site->init();

-->bootstrap.php

Code: Select all

<?php

class bootstrap{
    
    public $lang;
    public $class;
    public $method;
    
    public function init() {
        //echo 'qmlskdfjmqlksjdfmlqskjfmlqsjkfmlqjksfmlqsdfjk';
        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);

        //Set values on startup
        if($url[0] == NULL) {$url[0] = 'nl';}
        if($url[1] == NULL) {$url[1] = 'index';}

        if(isset($url[0])) {$this->lang = $url[0];}
        if(isset($url[1])) {$this->class = $url[1];}
        if(isset($url[2])) {$this->method = $url[2];}
        
        echo 'We are in the bootstrap <br>';
        
        $this->loadClass();
    }
    
    public function loadClass(){
        $class = $this->class;
        $newclass = new $class();
    }
    
}
-->index.php

Code: Select all

<?php

class index extends bootstrap{

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

    public function hello(){
        echo 'hello from the index class <br>';
    }

}
-->help.php

Code: Select all

<?php

class help extends bootstrap{

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

    public function hello(){
        echo 'hello from the help class <br>';
    }

}
All of it works. For example, when i input the url of "http://localhost/testing/nl/index" it then shows the message 'We are in the bootstrap' and 'hello from the index class'. The thing I don't understand is how the autoload function does it's thing.

Thanks in advance!
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: How to extending a class in another file

Post by mecha_godzilla »

Hi,

I think the __autoloader() function is called whenever PHP tries to instantiate an object that doesn't have a class defined for it yet. Under normal circumstances PHP would generate a fatal error if the class you're calling wasn't defined, but the __autoloader() function catches this exception and runs your custom code instead. If the class you're trying to load can't be found in the file that's been loaded, or the file that PHP is trying to load can't be found, a fatal error will still occur though. With that in mind, it would be a good idea to use file_exists() to test for situations where the URL someone is trying to access can't be mapped to a file - you could just create a generic "404" class and load that instead.

Just for future reference, the way that you've structured the code means that the bootstrap object and the index/help objects are separate - this may or may not be what you want to achieve, but if they're going to be separate entities then there's no real need for the "child" classes to extend the "parent" (bootstrap) class. I think what I'm trying to say here is that you'll have two instances of the "bootstrap" object running when you could just have one - as I mentioned before, if you instantiate any of the "child" classes they will automatically inherit the properties of the bootstrap class. For production purposes, you might just want to make the "bootstrap" class standalone and have the "child" classes extend a "functions" class instead, so that they can access the shared functions.

As an example:

-->functions.php

Code: Select all

class functions {

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

    public function hello(){
        // defined in the extended class
    }

}
-->help.php

Code: Select all

class help extends functions {

    public function hello(){
        echo 'hello from the help class <br>';
    }

}
In theory, when you go to "http://localhost/testing/nl/help" your bootstrap class should autoload the "help" class (which will then cause PHP to look for the "functions" class and autoload that) then call the __construct() method defined in the "functions" class to call the hello() method defined in the "help" class.

HTH,

M_G
Axllrod
Forum Newbie
Posts: 12
Joined: Tue Jun 12, 2012 4:15 pm
Location: Belgium

Re: How to extending a class in another file

Post by Axllrod »

The thing is, I was thinking about making a sort of application that could handle multiple languages (like english, dutch, french, german...) Depending on the language the site should then loaded dinamically. I started out by making the main class, the bootstrap class, and work from there out. So in the bootstrap class I would then break up the URL and depending on that i would load the controllers needed to generate the requested page. However, how I should handle all of that is still a bit blurry. I was thinking of using a basic MVC structure. There I could have fixed paths to fixed folders, I would only have to fill in the file name with a variable depending on the requested page.
Post Reply