Just remember about this site. o_O (Having an OOP issue)

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
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Just remember about this site. o_O (Having an OOP issue)

Post by Fractal »

Code: Select all

default: {
    $class->data_output("1");
  } break;
Fatal error: Call to a member function data_output() on a non-object in <file path && file_name> on line 53

Anybody know how to fix this?
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

show please the rest of the code...
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

duk wrote:show please the rest of the code...
If you mean show data_output()

Code: Select all

function data_output($action_id) {
    global $links, $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
    if ($action_id == "1") {
      if (isset($admin_access)) {
        openPanel(message("7"));
        echo $table_o;
        foreach($links as $link) {
          echo $tr_o.$td_o."<a href='{$link}'>$link_name</a><br />".$td_c.$tr_c;
        }
        echo $table_c;
      } else {
        openPanel(message("8"));
        echo $table_o.$tr_o.$td_o;
        // message("2")
        echo "You are not logged in.<br />";
        echo $td_c.$tr_c.$table_c;
      }
      closePanel();
    }

    if ($action_id == "2") {
      if ($_POST['login']) {
        if (isset($admin_access)) {
          if ($_POST['user_name'] == "Fractal") {
            if ($_POST['user_pass'] == "test") {
              openPanel(message("9"));
              set_status("1");
              echo $table_o.$tr_o.$td_o.message("5").$td_c.$tr_c.$table_c;
            } else {
              openPanel(message("10"));
              echo $table_o.$tr_o.$td_o.message("4").$td_c.$tr_c.$table_c;
            }
          } else {
            openPanel(message("11"));
            echo $table_o.$tr_o.$td_o.message("3").$td_c.$tr_c.$table_c;
          }
        } else {
          openPanel(message("9"));
          echo "<form action='?action=login' method='POST'>\n";
          echo $table_o;
          echo $tr_o.$td_o.message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o.message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o."<input type='submit' name='login' value='".message("15")."'><br />".$td_c.$tr_c;
          echo $table.c;
          echo "</form>\n";
        }
      } else {
        openPanel(message("12"));
        echo $table_o.$tr_o.$td_o.message("1").$td_c.$tr_c.$table_c."<br /><br />\n";
        echo "<form action='?action=login' method='POST'>\n";
        echo $table_o;
        echo $tr_o.$td_o.message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o.message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o."<input type='submit' name='login' value='".message("15")."'><br />".$td_c.$tr_c;
        echo $table.c;
        echo "</form>\n";
      }
      closePanel();
    }

    if ($action_id == "3") {
      global $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
      if (isset($admin_access)) {
        openPanel(message("16"));
        // set_status("2");
        echo $table_o.$tr_o.$td_o.message("6").$td_c.$tr_c.$table_c;
      } else {
        openPanel(message(8));
        echo $table_o.$tr_o.$td_o.message("2").$td_c.$tr_c.$table_c;
      }
      closePanel();
    }
  }
o_O

I'm just learning OOP so like yeah, all it's supposed to be doing is creating a basic login.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Nope. The code before and after:

Code: Select all

default: {
    $class->data_output("1");
  } break;
(#10850)
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

Code: Select all

<style type='text/css'>
a { text-decoration: none; color: orange; }
a:hover { color: #888; }
td { background-color: #CCC; color: #888; }
td.panel { border-color: #AAA; color: #888; }
</style>

Code: Select all

<?php

ob_start();

$lclass = "adminPanel";
function __autoload($lclass)
{
  if (!class_exists($lclass, false)) {
    trigger_error("Unable to load class: ".$lclass, E_USER_WARNING."\n");
  } elseif (class_exists($lclass)) {
    $admin = new $lclass();
  } else {
    trigger_error("Class ".$lclass." can and cannot be found.<br />\n");
  }
}

$links = array(
  1 => 'admin_test.php?action=',
  2 => 'admin_test.php?action=login&',
  3 => 'admin_test.php?action=logout&',
);

$link_name = array(
  'admin_test.php?action=', 'Home',
  'admin_test.php?action=login&', 'Login',
  'admin_test.php?action=logout&', 'Logout',
);

$admin_access = $_COOKIE['admin_access'];
$table_o = "<table width='100%' cellpadding='0' cellspacing='0'>\n";
$tr_o = "<tr>";
$td_o = "<td>";
$td_c = "</td>";
$tr_c = "</tr>\n";
$table_c = "</table>\n";

switch($_GET['action']) {
  case "login": {
    $admin->data_output("2");
  } break;

  case "logout": {
    $admin->data_output("3");
  } break;

  default: {
    $admin->data_output("1");
  } break;
}

class adminPanel {
  var $admin_access;

  function message($messno) {
    if ($messno == "1") return $this->data_output("You are already logged in.<br />");
    if ($messno == "2") return $this->data_output("You are not logged in.<br />");
    if ($messno == "3") return $this->data_output("The username you entered was incorrect.<br />");
    if ($messno == "4") return $this->data_output("The password you entered was incorrect.<br />");
    if ($messno == "5") return $this->data_output("You have logged in successfully.<br />");
    if ($messno == "6") return $this->data_output("You have logged out successfully.<br />");
    if ($messno == "7") return $this->data_output("Administration - Home<br />");
    if ($messno == "8") return $this->data_output("Administration - Error 2<br />");
    if ($messno == "9") return $this->data_output("Administration - Login<br />");
    if ($messno == "10") return $this->data_output("Administration - Error 4<br />");
    if ($messno == "11") return $this->data_output("Administration - Error 3<br />");
    if ($messno == "12") return $this->data_output("Administration - Error 1<br />");
    if ($messno == "13") return $this->data_output("Username<br />");
    if ($messno == "14") return $this->data_output("Password<br />");
    if ($messno == "15") return $this->data_output("Login<br />");
    if ($messno == "16") return $this->data_output("Administration - Logout<br />");
  }

  function set_status($statusno) {
    if ($statusno == "1") { /* Set Cookie */ }
    if ($statusno == "2") { /* Destroy Cookie */ }
  }

  function data_output($action_id) {
    global $links, $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
    if ($action_id == "1") {
      if (isset($admin_access)) {
        openPanel(message("7"));
        echo $table_o;
        foreach($links as $link) {
          echo $tr_o.$td_o."<a href='{$link}'>$link_name</a><br />".$td_c.$tr_c;
        }
        echo $table_c;
      } else {
        openPanel(message("8"));
        echo $table_o.$tr_o.$td_o;
        // message("2")
        echo "You are not logged in.<br />";
        echo $td_c.$tr_c.$table_c;
      }
      closePanel();
    }

    if ($action_id == "2") {
      if ($_POST['login']) {
        if (isset($admin_access)) {
          if ($_POST['user_name'] == "Fractal") {
            if ($_POST['user_pass'] == "test") {
              openPanel(message("9"));
              set_status("1");
              echo $table_o.$tr_o.$td_o.message("5").$td_c.$tr_c.$table_c;
            } else {
              openPanel(message("10"));
              echo $table_o.$tr_o.$td_o.message("4").$td_c.$tr_c.$table_c;
            }
          } else {
            openPanel(message("11"));
            echo $table_o.$tr_o.$td_o.message("3").$td_c.$tr_c.$table_c;
          }
        } else {
          openPanel(message("9"));
          echo "<form action='?action=login' method='POST'>\n";
          echo $table_o;
          echo $tr_o.$td_o.message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o.message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o."<input type='submit' name='login' value='".message("15")."'><br />".$td_c.$tr_c;
          echo $table.c;
          echo "</form>\n";
        }
      } else {
        openPanel(message("12"));
        echo $table_o.$tr_o.$td_o.message("1").$td_c.$tr_c.$table_c."<br /><br />\n";
        echo "<form action='?action=login' method='POST'>\n";
        echo $table_o;
        echo $tr_o.$td_o.message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o.message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o."<input type='submit' name='login' value='".message("15")."'><br />".$td_c.$tr_c;
        echo $table.c;
        echo "</form>\n";
      }
      closePanel();
    }

    if ($action_id == "3") {
      global $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
      if (isset($admin_access)) {
        openPanel(message("16"));
        // set_status("2");
        echo $table_o.$tr_o.$td_o.message("6").$td_c.$tr_c.$table_c;
      } else {
        openPanel(message(8));
        echo $table_o.$tr_o.$td_o.message("2").$td_c.$tr_c.$table_c;
      }
      closePanel();
    }
  }

  function openPanel($title) {
    echo "<table width='100%' cellpadding='0' cellspacing='1'>\n";
    echo "<tr><td class='panel'>".$title."</td></tr>\n";
    echo "<tr><td class='panel'>";
  }

  function closePanel() {
    echo "</td></tr>\n";
    echo "</table>\n";
  }
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$admin will not be magically created.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why are there brackets around your case statements in your switch? Maybe try this with them (just for clean coding, IMO)...

Code: Select all

switch($_GET['action']) {
  case "login": 
    $admin->data_output("2");
  break;

  case "logout": 
    $admin->data_output("3");
  break;

  default: 
    $admin->data_output("1");
  break;
}
Also, your creation of the $admin object is inside of an if statement inside of a function. Maybe try ditching the function. Check for the class, if it is not there, error out. Otherwise set it ...

Code: Select all

if (!class_exists($lclass, false)) {
    trigger_error("Unable to load class: ".$lclass, E_USER_WARNING."\n");
}

$admin = new $lclass();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I really isn't the place of __autoload to instantialize the classes.. Also $admin won't be in the local variable scope, and will only exist in that function (as feyd already said)
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

Fatal error: Call to undefined function openPanel() in <file path && file name> on line 92

Doesn't see it on like 92, but it see's it on line 85. o_O
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

to call a function (method) that is apart of the object, you must use $this, e.g.

Code: Select all

$this->openPanel('foo');
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

feyd wrote:to call a function (method) that is apart of the object, you must use $this, e.g.

Code: Select all

$this->openPanel('foo');
That's good to know, I'll change all the functions called to $this->[function_name]([title]); and see what happens.
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Ok, well it worked, but it's having real issues echoing it where it's supposed to be. As if it's echoing where the message function is located.
[url=http://img133.imageshack.us/my.php?image=error2ba.gif]Design Appearence[/url]

The updated code below (Removed function set_status() { } in the code below)

Code: Select all

function message($messno) {
    if ($messno == "1") echo "You are already logged in.<br />";
    if ($messno == "2") echo "You are not logged in.<br />";
    if ($messno == "3") echo "The username you entered was incorrect.<br />";
    if ($messno == "4") echo "The password you entered was incorrect.<br />";
    if ($messno == "5") echo "You have logged in successfully.<br />";
    if ($messno == "6") echo "You have logged out successfully.<br />";
    if ($messno == "7") echo "Administration - Home<br />";
    if ($messno == "8") echo "Administration - Error 2<br />";
    if ($messno == "9") echo "Administration - Login<br />";
    if ($messno == "10") echo "Administration - Error 4<br />";
    if ($messno == "11") echo "Administration - Error 3<br />";
    if ($messno == "12") echo "Administration - Error 1<br />";
    if ($messno == "13") echo "Username<br />";
    if ($messno == "14") echo "Password<br />";
    if ($messno == "15") echo "Login<br />";
    if ($messno == "16") echo "Administration - Logout<br />";
  }

  function data_output($action_id) {
    global $links, $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
    if ($action_id == "1") {
      if (isset($admin_access)) {
        $this->openPanel($this->message("7"));
        echo $table_o;
        foreach($links as $link) {
          echo $tr_o.$td_o."<a href='{$link}'>$link_name</a><br />".$td_c.$tr_c;
        }
        echo $table_c;
      } else {
        $this->openPanel($this->message("8"));
        echo $table_o.$tr_o.$td_o;
        echo $this->message("2");
        // echo "You are not logged in.<br />";
        echo $td_c.$tr_c.$table_c;
      }
      $this->closePanel();
    }

    if ($action_id == "2") {
      if ($_POST['login']) {
        if (isset($admin_access)) {
          if ($_POST['user_name'] == "Phathom") {
            if ($_POST['user_pass'] == "r0c3s") {
              $this->openPanel($this->message("9"));
              $this->set_status("1");
              echo $table_o.$tr_o.$td_o.$this->message("5").$td_c.$tr_c.$table_c;
            } else {
              $this->openPanel($this->message("10"));
              echo $table_o.$tr_o.$td_o.$this->message("4").$td_c.$tr_c.$table_c;
            }
          } else {
            $this->openPanel($this->message("11"));
            echo $table_o.$tr_o.$td_o.$this->message("3").$td_c.$tr_c.$table_c;
          }
        } else {
          $this->openPanel($this->message("9"));
          echo "<form action='?action=login' method='POST'>\n";
          echo $table_o;
          echo $tr_o.$td_o.$this->message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o.$this->message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
          echo $tr_o.$td_o."<input type='submit' name='login' value='".$this->message("15")."'><br />".$td_c.$tr_c;
          echo $table.c;
          echo "</form>\n";
        }
      } else {
        $this->openPanel($this->message("12"));
        echo $table_o.$tr_o.$td_o.$this->message("1").$td_c.$tr_c.$table_c."\n";
        echo "<form action='?action=login' method='POST'>\n";
        echo $table_o;
        echo $tr_o.$td_o.$this->message("13").$td_c.$td_o."<input type='text' name='user_name'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o.$this->message("14").$td_c.$td_o."<input type='password' name='user_pass'><br />".$td_c.$tr_c;
        echo $tr_o.$td_o."<input type='submit' name='login' value='".$this->message("15")."'><br />".$td_c.$tr_c;
        echo $table.c;
        echo "</form>\n";
      }
      $this->closePanel();
    }

    if ($action_id == "3") {
      global $admin_access, $table_o, $tr_o, $td_o, $td_c, $tr_c, $table_c;
      if (isset($admin_access)) {
        $this->openPanel($this->message("16"));
        $this->set_status("2");
        echo $table_o.$tr_o.$td_o.$this->message("6").$td_c.$tr_c.$table_c;
      } else {
        $this->openPanel($this->message(8));
        echo $table_o.$tr_o.$td_o.$this->message("2").$td_c.$tr_c.$table_c;
      }
      $this->closePanel();
    }
  }

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply