user2368229
user2368229

Reputation: 150

PHP Matrix Addition using classes

Alright, I'm new to Stack Overflow, and new-ish to PHP, and I'm just trying to get my head around PHP classes. Keep in mind, I'm used to C++.

What I'm trying to do, is create a Matrix (2x2) class, which seems to be done.

Then create 2 Matrices using this class, which also seems to be done.

Then add those 2 Matrices together, say a = a + b.

This is where I'm having a problem, I have a function to pass in the second Matrix I want to add, and all it's returning is the initial values for the first Matrix. (the one calling the function) Here's a simplified version of what I have:

class Matrix2
{
        private $m_m1;
        private $m_m2;
        private $m_m3;
        private $m_m4;

        function __construct($a_m1 = 1, $a_m2 = 1, $a_m3 = 1, $a_m4 = 1)
        {
            $this->m_m1 = $a_m1;
            $this->m_m2 = $a_m2;
            $this->m_m3 = $a_m3;
            $this->m_m4 = $a_m4;
        }

        public function AddMatrix(Matrix2& $Matrix)
        {
            $m_m1 + $Matrix2.$m_m1;
            $m_m2 + $Matrix2.$m_m2;
            $m_m3 + $Matrix2.$m_m3;
            $m_m4 + $Matrix2.$m_m4;        

            return $this;
        }
}

And when I'm calling it:

$MatrixA = new Matrix2();
$MatrixB = new Matrix2(2, 2, 2, 2);
var_dump($MatrixA->AddMatrix($MatrixB));

I know how to achieve this in C++, I have done many times, but the new syntax and such in PHP is confusing me.. Any help is appreciated :)

Upvotes: 2

Views: 784

Answers (1)

Jose Areas
Jose Areas

Reputation: 719

PHP has different syntax compared with C++. I will try to point what you are mixing. I will not check if your code works because I believe, logic is not a problem you have.

First of all, PHP has different ways to work with scope. Inside the function AddMatrix, you are setting value for the variables with function scope. I am assuming you want to set values to class variables. So, you must refer the current object.

public function AddMatrix(Matrix2& $Matrix)
        {
            $this->m_m1 + $Matrix2.$m_m1;
            $this->m_m2 + $Matrix2.$m_m2;
            $this->m_m3 + $Matrix2.$m_m3;
            $this->m_m4 + $Matrix2.$m_m4;        

            return $this;
        } 

Note I am using the operator -> to access a class Attribute instead of ".". Thats another problem in your code. The operator "." is used to concatenate string. To access class attributes or functions we use ->. So, check it out how will be your function after that:

public function AddMatrix(Matrix2& $Matrix)
        {
            $this->m_m1 + $Matrix2->m_m1;
            $this->m_m2 + $Matrix2->m_m2;
            $this->m_m3 + $Matrix2->m_m3;
            $this->m_m4 + $Matrix2->m_m4;        

            return $this;
        } 

Yes, I get rid of $ character after use ->. This is correct. We use $ only for a variable. Never when accessing object attributes.

Another mistake...PHP do not use datatype for variables. Objects are references already. So, the & operator is unnecessary. So, lets finish your function:

public function AddMatrix($Matrix2)
        {
            $this->m_m1 + $Matrix2->m_m1;
            $this->m_m2 + $Matrix2->m_m2;
            $this->m_m3 + $Matrix2->m_m3;
            $this->m_m4 + $Matrix2->m_m4;        

            return $this;
        } 

Upvotes: 2

Related Questions