Jump to content

[SOLVED] Passing values to constuctor in Object oriented?


Recommended Posts

Hey,

When I make an object, can i use some sort of parameters to pass values to the constructor?

If not, what can I do to "Set value X when its created" (value X= member variable)

 

 

code for object class:

<?php 
class UnitCounter
{//start class
protected $units = 0;
protected $weightPerUnit = 1.0;
function __construct($unit,$weight)
{
$this-> $units = $unit;
$this-> $weight = $weight;
}
function add($num = 1)  // Add $num to the total number of units
{
     return  $this->units = $this->units + $num;
}
function totalWeight()  // Calculate the total weight
  {
  return $this->units * $this->weightPerUnit;
}
}//end class
?>

 

 

I have parameters set up for the constuctor but I dont think thats right.

Also in the php statement i want something like this:

$bottles= new UnitCounter(1,3);

 

 

Or is the only way to make the object and then call a function to change the member variables $units and $weightPerUnit

I don't quite follow your question

but it all looks correct to me

 

 

only advice I have so far is ... wouldn't hurt to change this

return  $this->units = $this->units + $num;

to this

return  $this->units += $num;

<?php

function __construct($unit,$weight)
{
$this-> $units = $unit;
$this-> $weight = $weight;
}

?>

 

Should be

 

<?php

function __construct($unit,$weight)
{
   $this->units = $unit;
   $this->weight = $weight;
}

?>

Something is still wrong  ???

 

 

php file:

<?php
include('exam1p3.inc.php');
  $bottles= new UnitCounter(24,0);
  $books= new UnitCounter(10,1.5);
  $bottles->add(3);
  echo $bottles->$units."<br />";
  echo $books->totalWeight();
?>

 

included file code:

<?php 
class UnitCounter
{//start class
protected $units = 0;
protected $weightPerUnit = 1.0;
function __construct($unit,$weight)
{
   $this->units = $unit;
   $this->weight = $weight;
}
function add($num = 1)  // Add $num to the total number of units
{
     $this->units = $this->units + $num;
   
}

function totalWeight()  // Calculate the total weight
  {
  return $this->units * $this->weightPerUnit;
}
}//end class
?>

 

I need it to display the units of bottles and the totalweight of books.

Thanks in advance!

I fixed it and it still isnt showing any value when i run the php page.

 

 

php file:

<?php
include('exam1p3.inc.php');
  $bottles= new UnitCounter(24,0);
  $books= new UnitCounter(10,1.5);
  $bottles->add(3);
  echo $bottles->$units."<br />";
  echo $books->totalWeight();
?>

 

included file code:

<?php 
class UnitCounter
{//start class
$units = 0;
$weightPerUnit = 1.0;
function __construct($unit,$weight)
{
   $this->units = $unit;
   $this->weight = $weight;
}
function add($num = 1)  // Add $num to the total number of units
{
     $this->units = $this->units + $num;
   
}

function totalWeight()  // Calculate the total weight
  {
  return $this->units * $this->weightPerUnit;
}
}//end class
?>

 

I need it to display the units of bottles and the totalweight of books.

Thanks in advance!

 

This line

echo $bottles->$units."<br />";

 

Should be

echo $bottles->units."<br />";

 

Also, you should be using the "var" keyword to define your variables within the class.

 

<?php

var $units = 0;
var $weightPerUnit = 1.0;

?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.