简体   繁体   English

为一个班级提供2个其他班级的访问权限?

[英]providing a class with access to 2 other classes?

I have a login script which I found online which was written in php4 and have tried to modify it so that it is php5 complient. 我有一个在网上找到的登录脚本,该脚本是用php4编写的,并尝试对其进行修改,使其符合php5。

I have 4 classes: user, db, form, mailer 我有4个类别:user,db,form,mailer

below is a snippet of my user class 以下是我的用户类别的摘要

<?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'];
  }
}

and i invoke the database and form like so 我像这样调用数据库和表格

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

but it is throwing an error 但是它抛出一个错误

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

But I'm not sure why. 但是我不确定为什么。 If I remove the form $form from the construct function it works fine, but I need access to the form class 如果我从构造函数中删除表单$ form,它可以正常工作,但是我需要访问表单类

The why is easy. 为什么很容易。 Your constructor says this: 您的构造函数说:

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

That means you have to give it 2 things: something with class db and something with class Form . 这意味着您必须给它两件事: db类和Form类。

You call this: 您称之为:

$user = new User($db);

That does not have the Form , and that is exactly what you error says. 那没有Form ,而这正是您错误所言。 If you delete the Form from the constructor you don't expect it anymore, so the error isn't there, but you don't have the right functionality. 如果从构造函数中删除Form ,您将不再期望它,因此不会出现错误,但是您没有正确的功能。

What you should do is add the paramter to the constructor-call: 您应该做的是将参数添加到构造函数调用中:

    $user = new User($db, $form);

You are not supplying enough parameters to User::__construct() constructor. 您没有为User::__construct()构造函数提供足够的参数。 Take a look at declaration: 看一下声明:

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

It requires (as declared) 2 arguments: an instance of db class and an instance of Form class. 它需要(声明)两个参数: db类的实例和Form类的实例。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM