calling class vars in functions

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
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

calling class vars in functions

Post by invisibled »

ok so i'll try and explain this as simple as possible. The code below is a basicl example of what i'm trying to acomplish. Calling the foo var inside of the output function.

Code: Select all

 
class pagination{
  var $foo;
  function output(){
    global $foo;
 
   print $foo;
  }
}
 
and i define the foo variable in the page it gets called into like this.

Code: Select all

 
$p->foo = "blahblah";
$p->output();
 
now it doesnt give me any errors, but it doesnt display the 'blahblah' in the foo variable. I would like to do it in this method, as the class is meant to be abstracted. Im no whiz at classes and functions...

Any suggestions?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: calling class vars in functions

Post by Eran »

This is a misuse of the global keyword, you should be using the object reference $this inside class methods:

Code: Select all

class pagination{
   public $foo;
   function output(){
         $foo = $this -> foo;
         echo $foo;
    }
}
(I also spruced up the code to PHP5 standards. You are better off using it unless you are developing for existing legacy software)
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: calling class vars in functions

Post by invisibled »

ahhhh great! thanks.

So say i had 10 or so vars i wanted like this. Is there a more efficient way to write the code than this? Or is this the best/only way to do it?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: calling class vars in functions

Post by Eran »

Give a concrete example of what your class needs to do, and we'll see if there's a better way :wink:
invisibled
Forum Contributor
Posts: 112
Joined: Sun Apr 29, 2007 3:35 pm
Location: New Westminster

Re: calling class vars in functions

Post by invisibled »

oh well for this instance i will only have 2... maybe one more, i'm just building a pagination class for use on my own projects, but i'm trying to get good at writing class's and such so i was just wondering. But anyways here's what i have so far

The class

Code: Select all

class pagination{
    
    
    public $page_table;
    public $per_page;
    
    function output(){
        $page_table = $this->page_table;
        $per_page = $this->per_page;
        
        
        //The Query
        $page_query = mysql_query("SELECT id FROM $page_table");
 
        
 
        //Stores total number of results
        $num = mysql_num_rows($page_query);
 
 
 
        //Divides the number of results by the number per page
        $pages = $num / $per_page;
 
 
 
        //Sets the increment value at 1
        $i = "1";
        
    
        
        //The Output
        print '<div class="pagination">';
 
            print '<a href=""><</a>';
            
            while($i <= $pages):
                print '<a href="'.$php_self.'?page='.$i.'">'.$i.'</a>';
                $i++;
            endwhile;
 
            print '<a href="">></a>';
 
        print '</div>'; 
    }
}
and then i create the instance on the page like this:

Code: Select all

<?php 
        $p->page_table = "module_members";
        $p->per_page = "12";
        $p->output(); 
    ?>
Feel free to voice your opinion :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: calling class vars in functions

Post by Eran »

Regarding class configuration - if there aren't many properties, there's no problem with using class variables. If there are many, one alternative is to use a configuration array. The definition of "many" is very fluid, and differs plenty from developer to developer.

For example, configuration array:

Code: Select all

<?php
class Pagination {
    protected $config;
    public function setConfig($config) {
        $this -> config = $config;
    }
    
    public function output() {
        $page_table = $this -> config['page_table'];
        $per_page = $this -> config['per_page'];
        
        ...
    }
}
 
$config = array(
    'page_table' => $page_table,
    'per_page' => 10
);
$pagination = new Pagination();
$pagination -> setConfig($config);
For configuration it's common to use a constructor (a function which runs when the object is created, see the link):

Code: Select all

<?php
class Pagination {
    protected $config;
    
    public function __construct($config = null) {
        $this -> setConfig($config);    
    }
    
    public function setConfig($config) {...}
    
    public function output() {...}
}
 
$config = array(
    'page_table' => $page_table,
    'per_page' => 10
);
$pagination = new Pagination($config);
this setConfig() method should probably check that all the necessary configuration options are present and return an error if not.

A couple more points -

1. To count rows in mysql, it's best to run a COUNT query:

Code: Select all

$result = mysql_query("SELECT COUNT(*) AS count FROM " . $page_table);
$row = mysql_fetch_array($result);
$num = current($row);
Your original query basically scans the entire table, which could be slow depending on the size of the table.

2. It's good practice not to echo/print inside class methods, unless there is no chance for reuse (final output). Since you are calling the Pagination class from the global scope, the echo should be there. Concatenate your output and return it from the function. This way you could perform more manipulations on the string if you need to.

Code: Select all

<?php
class Pagination {
    public function __construct($config = null) {...}
    public function setConfig($config) {...}
    public function output(){
        $i = 1;
              
        $output = '<div class="pagination">'
                        . '<a href=""><</a>';
        while($i <= $pages):
            $output .= '<a href="'.$php_self.'?page='.$i.'">'.$i.'</a>';
            $i++;
        endwhile;
        $output .= '<a href="">></a>'
                . '</div>'; 
        return $output;
    }
}
 
$config = array(
    'page_table' => $page_table,
    'per_page' => 10
);
$pagination = new Pagination($config);
 
echo $pagination -> output(); // Output is echoed here
 
Post Reply