2011-02-09 48 views
0

我需要有两个文件。 让我们把文件控制的第二个:main.php和我们称之为辅助文件,一个与我的代码:second.php从外部文件传递参数到一个类

在second.php我:

<?php 

class tags{ 

    var $theuser; 
    var $thebarname; 
    var $theplace; 
    var $thetime; 
    var $themail; 

    function __construct($theuser,$thebarname,$theplace, $thetime, $themail){ 
     $this->theuser=$theuser; 
     $this->thebarname=$thebarname; 
     $this->theplace=$theplace; 
     $this->thetime=$thetime; 
     $this->themail=$themail; 

    } 

    function give_tags_theuser(){ 
     return $this->theuser; 
    } 
    function give_tags_thebarname(){ 
     return $this->thebarname; 
    } 

    function give_tags_theplace(){ 
     return $this->theplace; 
    } 

    function give_tags_thetime(){ 
     return $this->thetime; 
    } 
    function give_tags_themail(){ 
     return $this->themail; 
    } 
} 


$tags = new tags("John", "Starbucks", "NY", "4:30", "[email protected]"); 

$user= $tags->give_tags_theuser(); 
$barname = $tags->give_tags_thebarname(); 
$place = $tags->give_tags_theplace(); 
$time = $tags->give_tags_thetime(); 
$email = $tags->give_tags_themail(); 

// with the data before I will send an email using phpmailer, but that's another story 
?> 

在主。 PHP我需要将变量传递给类。这意味着我将删除:

$tags = new tags("John", "Starbucks", "NY", "4:30", "[email protected]"); 

从second.php,我会从main.php可以通过这个数据

什么我有second.php改变,以及如何将main.php是为了这样做?

如果我没有解释我自己,请告诉我,我仍然是一名学生,我可能还在搞乱词汇。

非常感谢

回答

0

如果您使用的类,那么你真不该在定义这些类里面的文件的CLASSE做操作。

例如给你的情况,你应该定义一个名为tags.php文件中的tags类,然后让你的main.php文件应用程序的亚军,所以在该文件中做:

require_once 'tags.php' 
$tags = new tags("John", "Starbucks", "NY", "4:30", "[email protected]"); 

$user= $tags->give_tags_theuser(); 
$barname = $tags->give_tags_thebarname(); 
$place = $tags->give_tags_theplace(); 
$time = $tags->give_tags_thetime(); 
$email = $tags->give_tags_themail(); 

这比写代码更好它在多个文件中运行你的应用程序。

+0

非常感谢! – user523129 2011-02-09 17:32:36