Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Method Not Working on Web Server in PHP

Tags:

oop

php

I just finished working on a site in PHP and decided to upload it unto my web server, but when I try to run pages with static method calls I get this error Unexpected T_PAAMAYIM_NEKUDOTAYIM. I know this has to do with double colon error, but I do not understand why it works on my local web server and refuses to work on my remote web server.

Due to this error, I had to through my whole site converting all static methods to instance methods

<?php
class User
{
    private $userId;

    public function __construct()
    {
        if(func_num_args() == 1)
        {
            $this->userId   =   func_get_arg(1);
        }
    }

    public static function userNameExists($name)
    {
        global $dbc;

        if(gettype($name) != "string")
        {
            die("Invalid Function Parameter");
        }

        $sql    =   "SELECT username FROM users WHERE users.username='$name'";
        $result =   mysqli_query($dbc, $sql) or die("Could Not Check Username at This Time: ".mysqli_error($dbc));

        if(mysqli_num_rows($result) > 0)
        {
            return true;
        }
        else
        {

            return false;
        }
    }
?>

When I call it:

<?php
   require("users.php");

   if(User::userNameExists($_GET['username']))
     header("Location:index.php");
?>
like image 957
James Okpe George Avatar asked Dec 09 '13 12:12

James Okpe George


3 Answers

You are missing a } in the end of your code,

<?php
class User
{
    private $userId;

    public function __construct()
    {
        if(func_num_args() == 1)
        {
            $this->userId   =   func_get_arg(1);
        }
    }

    public static function userNameExists($name)
    {
        global $dbc;

        if(gettype($name) != "string")
        {
            die("Invalid Function Parameter");
        }
        //Think about escaping user input
        $name = mysqli_real_escape_string($name);
        $sql    =   "SELECT username FROM users WHERE users.username='$name'";
        $result =   mysqli_query($dbc, $sql) or die("Could Not Check Username at This Time: ".mysqli_error($dbc));

        if(mysqli_num_rows($result) > 0)
        {
            return true;
        }
        else
        {

            return false;
        }
    }
}//This one
?>

You're also missing " in your require statement,

<?php
   require("users.php");//This one

   if(User::userNameExists($_GET['username']))
     header("Location:index.php");
?>
like image 143
ilyes kooli Avatar answered Sep 23 '22 21:09

ilyes kooli


If the exact same code is working locally, but gives a syntax error on a live server, you might be using different PHP versions (an old PHP4 version on the server, perhaps).

like image 40
Joel L Avatar answered Sep 23 '22 21:09

Joel L


You are missing a closing parenthesis at the end of

if(User::userNameExists($_GET['username'])

Do this instead

if(User::userNameExists($_GET['username']))

like image 34
Justin Paul Paño Avatar answered Sep 23 '22 21:09

Justin Paul Paño