Php 实例化类并在一行中声明变量

Php 实例化类并在一行中声明变量,php,class,instantiation,Php,Class,Instantiation,有没有一种方法可以实例化一个类并在一行中定义所有变量?实例化它,然后定义所有变量,然后调用它,占用的行数几乎与原始html代码本身占用的行数相同 我有一门课是这样的: class Gallery { public $image; public $text; public $heading; public $link; public function Item() { echo ' <div class="galler

有没有一种方法可以实例化一个类并在一行中定义所有变量?实例化它,然后定义所有变量,然后调用它,占用的行数几乎与原始html代码本身占用的行数相同

我有一门课是这样的:

class Gallery {
    public $image;
    public $text;
    public $heading;
    public $link;

    public function Item() {
        echo '
        <div class="gallery_item">
            <div class="gallery_image">
                <img src="' . $this->image . '">
            </div>
            <div class="gallery_text">
                <h3> ' . $this->heading . '</h3>
                <p> ' . $this->text . '</p>
                <a href="' . $this->link . '">View Live Site</a>
            </div>
        </div>
        ';
    }
}

您可以通过在类中放置
\u构造
函数来尝试这种方法

public function __construct()
 {
    $this->image = "img/test.jpg";
    $this->heading = "Test Object";
    $this->text = "This is a sample text description for the gallery";
    $this->link ="#";    
 }
然后只需创建您的库类的实例

    //will call the __construct method internally 
    $Gallery_1 = new Gallery(); 
    $Gallery_1->Item();
根据您的评论编辑:

  public function __construct($image,$heading,$text,$link) 
  {
    $this->image = $image;
    $this->heading = $heading;
    $this->text = $text;
    $this->link =link;    
  }

  $image = "img/test.jpg";
  $heading = "Test Object";
  $text = "This is a sample text description for the gallery";
  $link ="#"; 

  $Gallery_1 = new Gallery($image,$heading,$text,$link);.
  $Gallery_1->Item();

您可以通过在类中放置
\u构造
函数来尝试这种方法

public function __construct()
 {
    $this->image = "img/test.jpg";
    $this->heading = "Test Object";
    $this->text = "This is a sample text description for the gallery";
    $this->link ="#";    
 }
然后只需创建您的库类的实例

    //will call the __construct method internally 
    $Gallery_1 = new Gallery(); 
    $Gallery_1->Item();
根据您的评论编辑:

  public function __construct($image,$heading,$text,$link) 
  {
    $this->image = $image;
    $this->heading = $heading;
    $this->text = $text;
    $this->link =link;    
  }

  $image = "img/test.jpg";
  $heading = "Test Object";
  $text = "This is a sample text description for the gallery";
  $link ="#"; 

  $Gallery_1 = new Gallery($image,$heading,$text,$link);.
  $Gallery_1->Item();

如果要提供自己的值,请使用构造函数设置参数中的属性:

class Gallery 
{
    public $image;
    public $text;
    public $heading;
    public $link;

    public function __construct($image, $text, $heading, $link) // arguments
    {
        $this->image = $image;
        $this->text = $text;
        $this->heading = $heading;
        $this->link = $link;
    }

    public function Item()
    // rest of your codes
$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,
因此,在实例化时,只需填写参数:

class Gallery 
{
    public $image;
    public $text;
    public $heading;
    public $link;

    public function __construct($image, $text, $heading, $link) // arguments
    {
        $this->image = $image;
        $this->text = $text;
        $this->heading = $heading;
        $this->link = $link;
    }

    public function Item()
    // rest of your codes
$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,

如果要提供自己的值,请使用构造函数设置参数中的属性:

class Gallery 
{
    public $image;
    public $text;
    public $heading;
    public $link;

    public function __construct($image, $text, $heading, $link) // arguments
    {
        $this->image = $image;
        $this->text = $text;
        $this->heading = $heading;
        $this->link = $link;
    }

    public function Item()
    // rest of your codes
$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,
因此,在实例化时,只需填写参数:

class Gallery 
{
    public $image;
    public $text;
    public $heading;
    public $link;

    public function __construct($image, $text, $heading, $link) // arguments
    {
        $this->image = $image;
        $this->text = $text;
        $this->heading = $heading;
        $this->link = $link;
    }

    public function Item()
    // rest of your codes
$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,


在您的构造函数中添加参数,我刚才正在考虑python中的Kwargs或args,不知道我是否可以在这里做同样的事情。你能给我举一个正确语法的例子吗?有时我在这里读到的问题对于任何初学者来说似乎都是完全合理的,但由于一些奇怪的原因,他们在没有解释或批评的情况下被否决了。我选了这个来帮助平衡神秘的恶意。谢谢。不知道为什么会被否决。虽然我不是PHP的初学者,但我以前从未使用过类,我想开始更多地了解它们。我想经常使用它们会有助于理解。在构造函数中添加参数,我只是在想python中的Kwargs或args,不知道我是否可以在这里做同样的事情。你能给我举一个正确语法的例子吗?有时我在这里读到的问题对于任何初学者来说似乎都是完全合理的,但由于一些奇怪的原因,他们在没有解释或批评的情况下被否决了。我选了这个来帮助平衡神秘的恶意。谢谢。不知道为什么会被否决。虽然我不是PHP的初学者,但我以前从未使用过类,我想开始更多地了解它们。我想经常使用它们会有助于理解。课程的重点是我需要构建许多不同的图库项目,这些项目都有不同的图像、文本等。我应该在你的方法中将新对象的变量放在哪里?非常感谢!鬼也给出了同样的答案。我该把钱给谁?@Longblog,这取决于你,但我的名声比鬼还差。看了这两个答案后,我要给他打上正确的标记,因为我觉得代码更清晰了一点。不过我真的很感谢你的帮助。再次感谢@没问题。我不仅仅是完美的缩进代码,无论如何祝你好运。这个课程的重点是我需要构建许多不同的图库项目,它们都有不同的图像、文本等。我应该在你的方法中将新对象的变量放在哪里?非常感谢!鬼也给出了同样的答案。我该把钱给谁?@Longblog,这取决于你,但我的名声比鬼还差。看了这两个答案后,我要给他打上正确的标记,因为我觉得代码更清晰了一点。不过我真的很感谢你的帮助。再次感谢@没问题。我不仅仅是完美地缩进了代码,无论如何祝你好运。@Longblog如果你尝试将其调整为数组参数形式,这也会起作用,我很高兴这有助于感谢你的帮助。@Longblog一点问题也没有,很高兴我能这样做help@Longblog如果您尝试将其调整为数组参数形式,这也会起作用,我很高兴这能帮上忙谢谢你的帮助。@Longblog一点问题都没有,很高兴我能帮上忙