将参数从外部文件传递到类
我需要有两个文件。 让我们调用控制第二个文件的文件:main.php,让我们调用辅助文件,即包含我的代码的文件:second.php
在 secondary.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
?>
在 main.php 中,我需要将变量传递给类。这意味着我将从 secondary.php 中删除:
$tags = new tags("John", "Starbucks", "NY", "4:30", "[email protected]");
,并且我将从 main.php 传递此数据,
我必须在 secondary.php 中更改哪些内容以及 main.php 如何才能做到这一点?
如果我没有解释清楚,请告诉我,我仍然是一名学生,我可能还在搞乱词汇。
多谢
I need to have two files.
Let's call the file controlling a second one: main.php and let's call the secondary file, the one with my code: second.php
In second.php I have:
<?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
?>
In main.php I need to pass the variables to the class. Meaning that I will delete:
$tags = new tags("John", "Starbucks", "NY", "4:30", "[email protected]");
from second.php and I will be passing this data from main.php
What do I have to change in second.php and how would main.php be in order to do so?
If I did not explain myself, please tell me so, I'm still a student and I'm probably still messing up with the vocabulary.
Thanks a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用类,那么您实际上不应该对定义这些类的文件内的这些类进行操作。
例如,考虑到您的情况,您应该在名为
tags.php
的文件中定义tags
类,然后使您的main.php
文件成为您的运行程序应用程序,因此在该文件中执行以下操作:这比编写在多个文件中运行应用程序的代码更好。
If your using classes then you shouldn't really be doing operations on those classe inside the files that define those classes.
For example given your situation you should define the
tags
class inside a file calledtags.php
then make yourmain.php
file the runner of your application, so in that file do:This is better than writing code which runs your application in multiple files.