11

Here's a sample code that I'm trying to call from another page to help me with stats. I can't seem to get it to work. How do I import and call this class in another php file? stats.php

<?php

include("config.php");
$link = mysql_connect($host, $username, $password);
mysql_select_db("mydb", $link);

class stats{

  function newReg(){

    $result = mysql_query("SELECT * FROM people where status ='registered' ", $link);
    $num_rows = mysql_num_rows($result);
     return $num_rows ;


function newApp(){
    $result = mysql_query("SELECT * FROM people where status = 'NEW' ", $link);
    $num_rows = mysql_num_rows($result);

    return $num_rows;
 }
?>

I want to call that class here in another file:

<?php

require_once("stats.php");
   echo(stats.newReg());

 ?>

Is there something I'm missing here?

1
  • Without seeing the code where you try to include it in the other page, it's difficult to help... Commented Oct 28, 2011 at 13:13

2 Answers 2

15

you forgot 2 closing brackets

<?php

include("config.php");
$link = mysql_connect($host, $username, $password);
mysql_select_db("mydb", $link);

class stats{

  function newReg(){
    global $link;
    $result = mysql_query("SELECT * FROM people where status ='registered' ", $link);
    $num_rows = mysql_num_rows($result);
     return $num_rows ;
  }

function newApp(){
    global $link;        
    $result = mysql_query("SELECT * FROM people where status = 'NEW' ", $link);
    $num_rows = mysql_num_rows($result);

    return $num_rows;
 }
}
?>

anyway, other file:

include 'statsclassfile.php';
$myStats = new stats();
$mystats->newReg();

PS: naming conventions generally want that class names begin with a capital letter, eg: Stats

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your comment. But I'm getting <i>Fatal error: Call to a member function newReg() on a non-object in /home/default/helen/testStats.php on line 4</i>
put error_reporting(E_ALL); at the beginning of the file testStats.php and see if the script reports more details on why it doesn't instance your class. One question, are the 3 files (config.php, testStats.php and the other one) all in the same directory?
actually... $link is declared in global space. That means you cannot access it from within a function without declaring it there as global. I'll update my answer to include this change. (this has nothing to do with the include problem)
Thanks again for your amazing help. It's now working like a dream. You've been very helpful.
1

Make sure the paths are correct, maybe the config.php file is not on the same folder.

You can use absolute paths like this

require_once $_SERVER['DOCUMENT_ROOT'] . "/path_to_config/config.php";

And to call your stats class:

require_once PATH_TO_YOUR_CLASS . "stats.php"

Also make sure you name classes with CamelCased names.

1 Comment

It's actually PascalCase, not camelCase. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.