Novice OOP Class question

Hi,

It’s been quite a few years since I’ve dabbled with PHP. I’m just having a bit of a toe dipping exercise.

I’ve been leafing through a very dusty copy of, ‘Web Application Development with PHP and MySQL’. Specifically the OOP Section.

An example given for setting up a class is similar to the following

class Product
{
  protected $id;
  protected $cat;
  protected $name;
  protected $desc;
  protected $cas;
  protected $voltage;
  protected $price_per_unit;
  protected $location;
  
  public function __construct
  ( 
    $id,
    $cat,
    $name,
    $desc,
    $cas,
    $voltage,
    $price_per_unit,
    $location
  )
  
  { 
	$this->id = $id;
	$this->cat = $cat;
	$this->name = $name;
	$this->desc = $desc;
	$this->cas = $cas;
	$this->voltage = $voltage;
	$this->price_per_unit = $price_per_unit;
	$this->location = $location;
  }
  
  public function get_name()
  {
    return $this->name;
  }
  
  public function get_desc()
  {
    return $this->desc;
  }
}

$prod = new Product(1, 
                    5, 
					'F3-16000CL6T-6GBPID', 
					'DDR3 6GB 2000mhz Triple Channel Modules', 
					'6-9-6-24-2N', 
					1.65, 
					210, 
					'onsite');

echo 'Module name: '.$prod->get_name();
echo '<br>Description: '.$prod->get_desc();
?>

It’s nice and clear, but I just wondered given the repetition whether passing in an array to the constructor is a good alternative or not?

Could you add some sort of checks to make sure you have the necessary arguments?

Example:

<?php
class Product
{
  protected $id;
  protected $cat;
  protected $name;
  protected $desc;
  protected $cas;
  protected $voltage;
  protected $price_per_unit;
  protected $location;
  
  public function __construct
  ( // Arguments array
    $keys
  )
  
  { // Constructor properties
	foreach ($keys as $key => $value)
	{
	  $this->$key = $value;
	}
  }
  
  public function get_name()
  {
    return $this->name;
  }
  
  public function get_desc()
  {
    return $this->desc;
  }
}

$prod = new Product(array('id' => 1,
                          'cat' => 5,
						  'name' => 'F3-16000CL6T-6GBPID',
					      'desc' => 'DDR3 6GB 2000mhz Triple Channel Modules',
						  'cas' => '6-9-6-24-2N',
						  'voltage' => 1.65,
					      'price_per_unit' => 210,
					      'location' => 'onsite'));

echo 'Module name: '.$prod->get_name();
echo '<br>Description: '.$prod->get_desc();
?>

Cheers

RLM

I would go with the indexed array to set the initial properties you want and then use setters to set the rest of the properties when necessary.

if you use an associative array you will have to know the exact names of the properties in the class to set as the keys in the array

I understand indexed arrays, getters and setters etc, but how that’s used in the way you’re describing is a bit vague to me.

I’m going to order a couple of new PHP books I’ve got my eye on and get reading:)

Thanks

RLM

sorry :frowning: I mislead you a bit there.

when I saw


[B][COLOR=#000000][COLOR=#000000][B]public[/B][/COLOR] [COLOR=#000000][B]function[/B][/COLOR] __construct[/COLOR][/B][COLOR=#000000]
 
[B] [COLOR=#66cc66]([/COLOR] [COLOR=#ff9900][I]// Arguments array[/I][/COLOR][/B]
 
[B]   [COLOR=#0000cc]$keys[/COLOR][/B]
 
[B] [COLOR=#66cc66])[/COLOR][/B]
 
 
 
[B] [COLOR=#66cc66]{[/COLOR] [COLOR=#ff9900][I]// Constructor properties[/I][/COLOR][/B]
 
[B]   [COLOR=#006600]foreach[/COLOR] [COLOR=#66cc66]([/COLOR][COLOR=#0000cc]$keys[/COLOR] [COLOR=#006600]as[/COLOR] [COLOR=#0000cc]$key[/COLOR] => [COLOR=#0000cc]$value[/COLOR][COLOR=#66cc66])[/COLOR][/B]
 
[B]   [COLOR=#66cc66]{[/COLOR][/B]
 
[B]     [COLOR=#0000cc]$this[/COLOR]->[COLOR=#0000cc]$key[/COLOR] = [COLOR=#0000cc]$value[/COLOR];[/B]
 
[B]   [COLOR=#66cc66]}[/COLOR][/B]
 
[B] [COLOR=#66cc66]}[/COLOR][/B]
 
[/COLOR]



I got carried away and talked about indexed arrays when I meant just passing paramaters.

what I meant to post was

I would go with passing non array parameters to set the initial properties you want and then use setters to set the rest of the properties when necessary.

if you use an associative array you will have to know the exact names of the properties in the class to set as the keys in the array

I’m not sure what you’re looking for here, the way to pass an arbitrary number of variables to the constructor that will be converted to some sort of private/protected members or you want to pass an arbitrary number of variables and check whether those that were passed contain ones you mark as required? Anyway, in order to shorten your argument list in the constructor, you can go around the problem like this:

private $members;

public function __construct(array $arguments = null)
{
	if(!is_null($arguments))
	{
		foreach($arguments as $key => $value)
		{
			$this->$key = $value;
		}
	}
}

public function __set($key, $value)
{
	$this->members[$key] = $value;
}

I don’t know whether the explanation is needed here, but I’ll post one anyway:

So, the constructor can take an array or nothing as the parameter. If you pass a string and so on, you’ll get an error.

That way, you can now construct your object such as this:


$product = new Product( array(
							'id'		=> 1,
							'title'		=> 'Lorem Ipsum',
							'code'		=> '123'
						));

What setter will do is that it’ll set the innaccessible properties to certain values. Array indexes “id”, “title”, “code” are not defined as member variables in your class.
Hence, magic function __set() will be invoked and it’ll populate $members variable with the data.

If you need further processing, such as checking whether certain fields were passed to the constructor - it’s trivial, so I won’t post the example.

To be honest, I think it’s a case of me trying to run before I can walk. A bit of impulsive coding.

Thanks a lot for the feed back though. Both of you. I think the more I readup the more your explanations will fall into place.

One of the first things I will be checking out is that magic __set function.

All good:D

RLM