PHP Calling a Method from an external class file

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
6,013
Reaction score
1,199
Location
Canada
Hey everyone,

I am currently learning PHP, and I have a small question to ask. I got to object orientated programming, I get the idea behind it, define the class and the functions inside the class, and you can decide if variables (fields) inside the class be public/private/protected - same applies for the functions.

My question is, can one create a seperate file, which will just contain the class(es) you will use, a seperate file for the form, and then another file for processing the form (here, you will somehow call the classes file you made).

Is this possible in PHP?

I thought a simple include "filename.php" (filename is the file which has the classes you may use). I tried this, but then it gave me an error saying my class does not exist.
 
I'm no PHP expert so I may be way off but have you tried require instead of include? (And what you are trying to do should be possible)
 
You can definitely use require or include. If you're getting that error, make sure you're referencing the file right (path).
 
Yes you can. Typically you'd have this:

index.php
Code:
<?php
require "classes.php";
//code here for index.php
//code creating an object from classes.php

//end code
?>

Just make sure the classes.php file is on the same server. Usually external files are blocked for security reasons.
 
Yes, Koeks, you can.

Firstly, create a file, and call it functions.php. Here you can add your functions, and remember to include your db if you use MySQL. Take a look at the sample below:

Code:
<?php

ini_set('default_charset', 'UTF-8');
require 'db.php';

class User {

    function get_users() {
        $query = mysql_query("SELECT * FROM `xxxxx`");
        while ($row = mysql_fetch_assoc($query)) {
            $newarray[] = $row;
        }
        return $newarray;
    }

    function get_moreusers() {
        $query = mysql_query("SELECT * FROM `xxxx` where xxxxxx!= xxxxxx");
        while ($row = mysql_fetch_assoc($query)) {
            $newarray[] = $row;
        }
        return $newarray;
    }
}
?>

In your script or site, you will simply include the functions.php file and call the functions where you need it.

Code:
<?php
                    include 'functions.php';
                    $user = new User();
                    $test = $user->get_users();
                    
.....

                    }
                    ?>
 
Last edited:
Do yourself a favour and learn hack, if you're going that **** language route.
 
PHP supports including files through require and include.

One thing to note is that classes and methods can only be declared once within the scope of a running program. This simply means that you will not be able to include a file with a class definition twice.

But while you're learning, PSR-1 may be useful to learn about. It deals with autoloading.

Take note that OOP is not about classes: it refers to using design patterns to achieve a goal, i.e. Mediator, Command, Builder. etc.
This concept becomes more apparent when using languages which do not support classes, such as JavaScript.
 
Thanks a lot guys,

I did manage to sort it out - an include or require statement works. I noticed though if I say:

include ($filePath) or die ("File cannot be found");

it returns an error saying it cannot find the class I want. Is the above line of code legal?
 
Thanks a lot guys,

I did manage to sort it out - an include or require statement works. I noticed though if I say:

include ($filePath) or die ("File cannot be found");

it returns an error saying it cannot find the class I want. Is the above line of code legal?

Depends what's in $filePath. If I take the name of the variable literally, mabey it's not the full file name, but only the "path" to the file name?. :) Careful how you name variables. Or cut and paste code from the internet :)

If the file containing your classes is in the same directory then you don't need to include the path in the include. If it's in a different directory then maybe that's where your problem is. You need to include the directory or the relative directory. For classes you should really use include_once for this sort of thing.
 
Last edited:
It may be obvious to you, but in case its not, if your files are not in the same folder, set it up so that you can use relative paths to avoid rehashing later.
 
use dirname(__FILE__) to return the full path to the current file you're coding in and use that as a base to find the file you're trying to include...

Code:
<?php
require_once dirname(__FILE__).'/../relative/path/to/class/file.php' or die("Can't find file");
...
 
Top
Sign up to the MyBroadband newsletter
X