将参数从外部文件传递到类

发布于 2024-10-16 15:36:05 字数 1821 浏览 0 评论 0原文

我需要有两个文件。 让我们调用控制第二个文件的文件: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-10-23 15:36:05

如果您使用类,那么您实际上不应该对定义这些类的文件内的这些类进行操作。

例如,考虑到您的情况,您应该在名为 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();

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

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 called tags.php then make your main.php file the runner of your application, so in that file do:

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();

This is better than writing code which runs your application in multiple files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文