2013-10-16 3 views
0

메신저 꽤 phop 클래스 등등에 새로워주세요. 내게 맨손으로 제발 :)다른 2 개의 클래스에 대한 액세스 권한이있는 클래스를 제공합니까?

나는 php4로 작성되었으며 php5 complient가되도록 수정하려고 시도한 온라인 로그인 스크립트가 있습니다.

전 4 개 수업을 : 사용자, DB, 양식, 우편물을 아래

내 사용자 클래스

<?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 

하지만 확실하지 않은 이유는 무엇입니까? 내가 그것을 잘 작동하는 구조 함수에서 양식 $ 양식을 제거 할 수 있지만 내가 폼 클래스

에 액세스해야하는 경우 도움 주시면 감사하겠습니다 :)

답변

2

을 당신은 User::__construct() 생성자에 매개 변수를 충분히 공급되지 않은 : 당신이 무엇을해야

생성자 호출에 있었던 파라미터를 추가 할 수 있습니다. 선언에서보세요 : db 클래스의 인스턴스와 Form 클래스의 인스턴스 (선언)

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

그것은이 개 인수가 필요합니다.

이 시도 :

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

도움을 주셔서 대단히 감사드립니다! – user2886669

2

쉬운 이유. 클래스 Form와 클래스 db 뭔가 뭔가 : 당신이 2 가지를 제공해야 의미

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

: 귀하의 생성자이 말했다.

$user = new User($db); 

Form이없는 즉, 그것은 오류가 말한다 정확히 당신입니다 :

이를 호출합니다. 생성자에서 Form을 삭제하면 더 이상 기대하지 않으므로 오류가 없지만 올바른 기능을 사용할 수 없습니다.

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

대단히 감사합니다! 당신의 도움은 대단히 감사합니다 !! – user2886669

관련 문제