0

我有一个登录脚本,我在网上找到它是用 php4 编写的,并试图修改它以使其符合 php5。

我有 4 个类:用户、数据库、表单、邮件程序

下面是我的用户类的片段

<?php
include("include/database.php");
include("include/mailer.php");
include("include/form.php");

include("constants.php");

class user
{
var $username;     //Username given on sign-up
var $firstname;
var $lastname;
var $userid;       //Random value generated on current login
var $userlevel;    //The level to which the user pertains
var $time;         //Time user was last active (page loaded)
var $logged_in;    //True if user is logged in, false otherwise
var $userinfo = array();  //The array holding all user info
var $url;          //The page url current being viewed
var $referrer;     //Last recorded site page viewed
var $num_active_users;   //Number of active users viewing site
var $num_active_guests;  //Number of active guests viewing site
var $num_members;        //Number of signed-up users

/**
    * Note: referrer should really only be considered the actual
    * page referrer in process.php, any other time it may be
    * inaccurate.
    */

public function __construct(db $db, Form $form)
{
    $this->database = $db;
    $this->form = $form;
    $this->time = time();
    $this->startSession();

    $this->num_members = -1;

    if(TRACK_VISITORS)
    {
        /* Calculate number of users at site */
        $this->calcNumActiveUsers();

        /* Calculate number of guests at site */
        $this->calcNumActiveGuests();
    }


}   
   /**
   * startSession - Performs all the actions necessary to 
   * initialize this session object. Tries to determine if the
   * the user has logged in already, and sets the variables 
   * accordingly. Also takes advantage of this page load to
   * update the active visitors tables.
   */
  function startSession()
  {
    session_start();   //Tell PHP to start the session

    /* Determine if user is logged in */
    $this->logged_in = $this->checkLogin();

    /**
    * Set guest value to users not logged in, and update
    * active guests table accordingly.
    */
    if(!$this->logged_in)
    {
        $this->username = $_SESSION['username'] = GUEST_NAME;
        $this->userlevel = GUEST_LEVEL;
        $this->addActiveGuest($_SERVER['REMOTE_ADDR'], $this->time);
    }
    /* Update users last active timestamp */
    else
    {
        $this->addActiveUser($this->username, $this->time);
    }

    /* Remove inactive visitors from database */
    $this->removeInactiveUsers();
    $this->removeInactiveGuests();

    /* Set referrer page */
    if(isset($_SESSION['url']))
    {
         $this->referrer = $_SESSION['url'];
    }
    else
    {
        $this->referrer = "/";
    }

    /* Set current url */
    $this->url = $_SESSION['url'] = $_SERVER['PHP_SELF'];
  }
}

我像这样调用数据库和表单

$db = new db($config);
$user = new User($db);
$form = new Form;

但它抛出了一个错误

Catchable fatal error: Argument 2 passed to user::__construct() must be an instance of Form, none given, called in C:\wamp\www\ecornwall3\include\user.php on line 900 and defined in C:\wamp\www\ecornwall3\include\user.php on line 30

但我不确定为什么。如果我从构造函数中删除表单 $form 它工作正常,但我需要访问表单类

4

2 回答 2

2

原因很简单。你的构造函数是这样说的:

public function __construct(db $db, Form $form)

这意味着你必须给它两件事:一些与 classdb和一些与 class Form

你称之为:

$user = new User($db);

那没有Form, 这正是你的错误所说的。如果您Form从构造函数中删除,您将不再期望它,因此错误不存在,但您没有正确的功能。

您应该做的是将参数添加到构造函数调用中:

    $user = new User($db, $form);
于 2013-10-16T14:12:25.893 回答
2

您没有为User::__construct()构造函数提供足够的参数。看一下声明:

public function __construct(db $db, Form $form)

它需要(如声明的那样)2 个参数:一个db类的实例和一个类的实例Form

尝试这个:

$db = new db($config);
$form = new Form;
$user = new User($db, $form);
于 2013-10-16T14:13:10.990 回答