Solved

How to pass array to class method in PHP?

Posted on 2014-11-27
4
21 Views
Last Modified: 2016-05-25
I'm currently looking to create a class to handle basic calculations. Can't seem to pass an array to the class assomething in my syntax is not right:
<?php

include "fin_class.php";

$subject1 = 10;
$subject2 = 2;
$subject_array= array(3, 5, 12);

$calculator = new calc;

$result = $calculator->add($subject1, $subject2, $subject_array);

echo $result;
?>

Open in new window


Heres the class:
<?php

class calc {

	var $amount1,
	    $amount2,
	    $array_amounts,
	    $vat_amt,
	    $vat_perc,
	    $nrl_amt,
	    $total,
	    $sub_total;

	    function add($amount1, $amount2, $array_amounts){
	    	if(isset($this->$array_amounts)){
	    		$total = array_sum($this->$array_amounts);
	    			    	} else {
	    	    $total = $amount1 + $amount2;
	    	}

	    	return $total;

	    }
}

Open in new window


I want the method to add two values, but occasionally there are more than two values and in this case they will be in the form of an array. The idea is to total the values in the array when it has values assigned to it.

I did think about just using an array regardless of the number of values and adding the values in the array. Perhaps there is a case to be made for a method that totals the values in an array instead of relying on the add method?
0
Comment
Question by:Mulith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 40469583
Check your syntax, it is $this->array_amounts. Which is not the problem anyway. Use the local scope of the arguments. And if you are using isset, you should use the first element of the array

        function add($amount1, $amount2, $array_amounts)
        {
            if(isset($array_amounts[0]))
            {
                $total = array_sum($array_amounts);
            } 
            else 
            {
                $total = $amount1 + $amount2;
            }

            return $total;

        }

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40469732
I think your instincts are good about placing all of the values you want to sum up into an array.  That would be the expected design pattern.  And you don't need OOP for this at all.  You can use array_sum() to get the answer in a single instruction.

If you're interested in learning PHP object-oriented programming, start your adventure here:
http://php.net/manual/en/language.oop5.php

In particular, try to stay away from internet examples.  The "good work" in PHP OOP design and programming is being done in systems like Laravel, and it's all very, very new in PHP.  An unfortunately large amount of really bad PHP code is lying around on the internet, so check the publication dates and steer yourself away from anything more than a year or two old.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41615796
For anyone who comes across this question in the future, please take note of the author's statement here:
Perhaps there is a case to be made for a method that totals the values in an array instead of relying on the add method?
The solution is found in using array_sum(), a PHP built-in function.  No other code is needed at all.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question