snowman15 Posted October 25, 2007 Share Posted October 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/ Share on other sites More sharing options...
Zane Posted October 25, 2007 Share Posted October 25, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377857 Share on other sites More sharing options...
snowman15 Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks, So using that code, Does this look good? $bottles= new UnitCounter(24,0); $books= new UnitCounter(10,1.5); echo $bottles->add(3); echo $books->totalWeight(); Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377858 Share on other sites More sharing options...
pocobueno1388 Posted October 25, 2007 Share Posted October 25, 2007 <?php function __construct($unit,$weight) { $this-> $units = $unit; $this-> $weight = $weight; } ?> Should be <?php function __construct($unit,$weight) { $this->units = $unit; $this->weight = $weight; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377860 Share on other sites More sharing options...
snowman15 Posted October 25, 2007 Author Share Posted October 25, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377863 Share on other sites More sharing options...
Zane Posted October 25, 2007 Share Posted October 25, 2007 you won't be able to echo units and weight when they're protected protected $units = 0; protected $weightPerUnit = 1.0; EDIT What were your results for this last time anyway Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377869 Share on other sites More sharing options...
snowman15 Posted October 25, 2007 Author Share Posted October 25, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377898 Share on other sites More sharing options...
pocobueno1388 Posted October 25, 2007 Share Posted October 25, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377914 Share on other sites More sharing options...
snowman15 Posted October 25, 2007 Author Share Posted October 25, 2007 thanks to all! it works! Quote Link to comment https://forums.phpfreaks.com/topic/74747-solved-passing-values-to-constuctor-in-object-oriented/#findComment-377919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.