0

Is there a way to instantiate a class and define the variables all on one line? Instantiating it, then defining all the variables, then calling it, is taking up almost as many lines as the raw html code would by itself.

I have a class like so:

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> '; } } 

And then I call it by using:

<?php $Gallery_1 = new Gallery(); $Gallery_1->image = "img/test.jpg"; $Gallery_1->heading = "Test Object"; $Gallery_1->text = "This is a sample text description for the gallery items. It can be long or short"; $Gallery_1->link ="#"; $Gallery_1->Item(); ?> 

Can I do something like below instead? The below code doesn't work, but is there something similar?

$Gallery_1 = new Gallery(image->"img/test.jpg", heading->"test", text->"text", link->"#"); $Gallery_1-Item(); 
4
  • 1
    add arguments inside your constructor Commented Apr 11, 2015 at 2:24
  • Yes, i was just thinking about Kwargs or args from python and wondering if I could do the same thing here. Could you give me an example of the proper syntax? Commented Apr 11, 2015 at 2:33
  • 1
    sometimes i read questions here that seem perfectly legitimate for any beginner to ask but for some odd reason they are downvoted without explanation or criticism. i upvoted this one to help balance the mysterious bad mindedness. Commented Apr 11, 2015 at 2:53
  • Thanks. No idea why it would be downvoted. While I'm not a beginner to PHP, I've never used classes before and want to start understanding them more. I figured using them often will help with the comprehension. Commented Apr 11, 2015 at 2:58

2 Answers 2

2

If you want to feed your own values, use your constructor to setup the properties in the arguments:

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 

So that when you instantiate, just fill up the arguments:

$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#'); $gallery->Item(); // echoes the html markup, 
Sign up to request clarification or add additional context in comments.

2 Comments

@Longblog this will also work if you try to tweak it into an array argument form, im glad this helped
@Longblog no problem at all, just glad i was able to help
2

You can try this way by putting a __construct function within your class.

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 ="#"; } 

Then just create instance of your Gallery class,

 //will call the __construct method internally $Gallery_1 = new Gallery(); $Gallery_1->Item(); 

EDIT: As per your comment

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

5 Comments

Well the point of the Class is that I need to construct many different gallery items which will all have different images, text, etc. Where do I put the variables for the new object in your method?
Great thank you! Ghost put the same answer up too. Who do I give the money to?
@Longblog, this is up to you but my reputation is less than ghost.
After looking at both answers I'm going to mark his correct because I feel like the coding is a a bit more clear. I really do appreciate your help though. Thanks again!
@Longblog No Prob. i was not just indented code perfectly, Anyways best of luck .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.