Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Can you pass variables into a Class in PHP?

I'm working through a tutorial on Node where I've got an MCV architecture going on.

This is the relevant piece of my Controller:

const Product = require("../models/product");

exports.postAddProduct = (req, res, next) => {
  const product = new Product(req.body.title); // right here I'm establishing a new instance of my Product class and I'm passing a variable into that class that's coming from my form
  product.save();
  res.redirect("/");
};

Open in new window


This is the relevant portion of my Model:

const products = [];

module.exports = class Product {
  constructor(title) {
    this.title = title;
  }

  save() {
    products.push(this);
  }

Open in new window


What you see works and there's nothing about it that's especially confusing, but I did have a question.

When I saw this syntax, I was able to understand its logic based on my experience with PHP, as far as creating an instance of a class and having a constructor sitting at the top of that class.

All good.

But that I started looking at it a little bit closer and noticed that I was passing a variable into a class.

You don't do that with PHP do you?

I know that's a bizarre question, but when I think of constructors in PHP, I'm thinking of defining some properties and the attaching values to those properties that I can then access throughout my class depending on their visibility.

But do you  / can you pass variables into a class in PHP?

There you go! A random question to kick off your Thursday morning...
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Absolutely you can. Here's a code example showing a variety of ways of passing in variables, some in constructors, some with defaults, etc...

<?php
class MyClass
{
  private $instanceNumber;
  private static $instanceCounter = 0;
  
  private $fav_color;
  private $favorite_food;
  private $locker_combo;
  public $other_notes;

  public function __construct($favorite_color, $favorite_food, $locker_combination = 123)
  {
    $this->instanceNumber = ++self::$instanceCounter;
    
    $this->fav_color = $favorite_color;
    $this->favorite_food = $favorite_food;
    $this->locker_combo = $locker_combination;
  }

  public function DisplayStuff()
  {
    echo "MyClass #" . $this->instanceNumber . ":\n";
    echo "  Favorite Color: " . $this->fav_color . "\n";
    echo "  Favorite Food: " . $this->favorite_food . "\n";
    echo "  Locker Combination: " . $this->locker_combo . "\n";
    echo "  Other Notes: " . $this->other_notes . "\n";
    echo "\n";
  }
}

$MC1 = new MyClass("Green", "Watermelon");
$MC1->DisplayStuff();

$MC2 = new MyClass("Red", "Apple", 12345);
$MC2->DisplayStuff();

$MC3 = new MyClass("Orange", "Orange", 54321);
$MC3->other_notes = "ORANGES!";
$MC3->DisplayStuff();

Open in new window


Output:
MyClass #1:
  Favorite Color: Green
  Favorite Food: Watermelon
  Locker Combination: 123
  Other Notes: 

MyClass #2:
  Favorite Color: Red
  Favorite Food: Apple
  Locker Combination: 12345
  Other Notes: 

MyClass #3:
  Favorite Color: Orange
  Favorite Food: Orange
  Locker Combination: 54321
  Other Notes: ORANGES!

Open in new window

Avatar of Bruce Gust

ASKER

OK, Gonzo! I get that, but could you do something like this:

$MC1 = new MyClass($color, $fruit);
$MC1->DisplayStuff();
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was my question, gentlemen, and you answered it.

Thanks!
You are welcome Bruce.