Coder Perfect

Message of error Strict guidelines: In PHP, non-static methods should not be called statically.


The following php is what I have. When I look at index.php, however, I receive the following error notice.

I’m hoping someone can help me figure out what’s wrong.


// { common variables and functions

// { get current page id
    if($page){ // load by name
        if($r && isset($r->id))$id=$r->id;
    if(!$id){ // else load by special
            if($r && isset($r->id))$id=$r->id;

// { load page data
    $PAGEDATA=(isset($r) && $r)?$r : Page::getInstance($id);
    echo '404 thing goes here';


require dirname(__FILE__).'/basics.php';


    function __autoload($name) {
        require $name . '.php';


class Page{
    static $instances             = array();
    static $instancesByName     = array();
    static $instancesBySpecial   = array();
    function __construct($v,$byField=0,$fromRow=0,$pvq=0){
        # byField: 0=ID; 1=Name; 3=special
        if (!$byField && is_numeric($v)){ // by ID
            $r=$fromRow?$fromRow:($v?dbRow("select * from pages where id=$v limit 1"):array());
        else if ($byField == 1){ // by name
            $r=dbRow("select * from pages where name like '".addslashes($name)."' limit 1");
        else if ($byField == 3 && is_numeric($v)){ // by special
            $r=dbRow("select * from pages where special&$v limit 1");
        else return false;
        if(!count($r || !is_array($r)))return false;
        if(!isset($r['name']))$r['name']='NO NAME SUPPLIED';
        foreach ($r as $k=>$v) $this->{$k}=$v;
        self::$instances[$this->id] =& $this;
        self::$instancesByName[preg_replace('/[^a-z0-9]/','-',strtolower($this->urlname))] =& $this;
        self::$instancesBySpecial[$this->special] =& $this;
    function getInstance($id=0,$fromRow=false,$pvq=false){
        if (!is_numeric($id)) return false;
        if (!@array_key_exists($id,self::$instances)) self::$instances[$id]=new Page($id,0,$fromRow,$pvq);
        return self::$instances[$id];
    function getInstanceByName($name=''){
        if(@array_key_exists($nameIndex,self::$instancesByName))return self::$instancesByName[$nameIndex];
        self::$instancesByName[$nameIndex]=new Page($name,1);
        return self::$instancesByName[$nameIndex];
    function getInstanceBySpecial($sp=0){
        if (!is_numeric($sp)) return false;
        if (!@array_key_exists($sp,$instancesBySpecial)) $instancesBySpecial[$sp]=new Page($sp,3);
        return $instancesBySpecial[$sp];

Asked by shin

Solution #1

The static keyword is missing from your methods. Change

function getInstanceByName($name=''){


public static function getInstanceByName($name=''){

If you want to call them statically, that’s what you’ll get.

Static methods (and Singletons) are testability’s worst enemy.

Also, you’re doing entirely too much work in the function Object() { [native code] }, particularly all that querying, which shouldn’t be there. Your constructor’s sole purpose is to put the object in a usable state. If you need data from outside the class to accomplish this, try injecting it rather than retrieving it. It’s also worth noting that constructors aren’t allowed to return anything. They will always return void, therefore all of these return false assertions will result in the construction being terminated.

Answered by Gordon

Solution #2

This, I believe, will address your question.

Non-static methods should not be referred to as statically.

If the method is not static you need to initialize it like so:

$var = new ClassName();

You can also use this syntax in PHP 5.4+:

(new ClassName)->method();

Answered by Pea

Solution #3

If scope resolution:: must be used outside of the class, the function or variable in question should be marked static.

class Foo { 
        //Static variable 
        public static $static_var = 'static variable'; 
        //Static function 
        static function staticValue() { return 'static function'; } 

        function Value() { return 'Object'; } 

 echo Foo::$static_var . "<br/>"; echo Foo::staticValue(). "<br/>"; $foo = new Foo(); echo $foo->Value();

Answered by Ravi Krishnan

Solution #4

Try this:

$r = Page()->getInstanceByName($page);

In a similar situation, it worked for me.

Answered by Andrés Frías

Solution #5

instead of className::function(), use className->function();

Answered by ulas korpe

Post is based on