Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP OOP - Including required files properly

Tags:

oop

php

Coming from a strong background in ActionScript 3, the process of requiring/importing a class that will be referenced / used / extended in another class is as follows:

import package.inner.ClassName;

This is required at the top of every external class file that will make use of ClassName, meaning it's required if I do any of the following, for example:

  • public class NewClass extends ClassName
  • var someVar:ClassName = new ClassName();
  • function someMethod():ClassName{ .. }

etc

I've got a fair bit of knowledge with PHP, ie I know how to use it to work with MySQL, output text, write functions, set up some basic classes, etc.

I've always just had one main class for small websites, which I create an instance of on the web-page at the top and work with from there, eg:

<?php
    class Website
    {
        public function __construct()
        {
            // connect to mysql, etc
        }

        public function example()
        {
            echo "example text";
        }
    }
?>

And then:

<?php
    require "whatever/class.website.php";
    $website = new Website();
?>
<!doctype html>
<html>
    <head>
        <title>Example</title>
    </head>
    <body>
        <?php
            $website->example();
        ?>
    </body>
</html>

This is fine, but now I want to start moving onto some larger projects with multiple classes. At first I was going OK, but after trying to set up a base class for multiple classes, I've run into a problem:

As an example, I've set up the following base class:

class Base
{
    public function test($input)
    {
        echo $input;
    }
}

From there I set up a class that extends this:

require "class.base.php";

class ExampleOne extends Base
{
    public function __construct()
    {
        $this->test("example one");
    }
}

And then checked to see if all worked as expected, which it did:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>
    </head>

    <body>
        <?php
            require "class.exampleone.php";

            $one = new ExampleOne();
        ?>
    </body>
</html>

I then went on to create an additional class that extends Base, which is where I ran into problems..

require "class.base.php";

class ExampleTwo extends Base
{
    public function __construct()
    {
        $this->test("example two");
    }
}

When I tried to create an instance of this class, or even just use require to gain access to the class, I get an error:

Fatal error: Cannot redeclare class Base in C:\xampp\htdocs\testing\class.base.php on line 3

So admittedly this error is pretty self explanatory, it's because I've used require "class.base.php"; at the top of each class - which is what you would do in ActionScript 3 because I'm making use of the base class (extending it).

I guess I'm just finding it hard to understand how I should be doing this. Where should I be putting my require for Base? It seems strange to omit it in the latter class I've extended, because if I don't make an instance of my first class then the base class won't be included at all.

Any advice would be awesome!

like image 412
Marty Avatar asked Mar 02 '26 23:03

Marty


1 Answers

You can use the require_once() statement. It functions just like the require statement, but will check to make sure that the base class hasn't been included yet and will load it if needed.

http://php.net/manual/en/function.require-once.php

You may also want to look into web frameworks depending on the size of your website, but that's outside of the question scope :)

like image 75
JackWink Avatar answered Mar 04 '26 11:03

JackWink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!