oop - PHP Matrix Addition using classes -


alright, i'm new stack overflow, , new-ish php, , i'm trying head around php classes. keep in mind, i'm used c++.

what i'm trying do, create matrix (2x2) class, seems done.

then create 2 matrices using class, seems done.

then add 2 matrices together, = + b.

this i'm having problem, have function pass in second matrix want add, , it's returning initial values first matrix. (the 1 calling function) here's simplified version of 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 achieve in c++, have done many times, new syntax , such in php confusing me.. appreciated :)

php has different syntax compared c++. try point mixing. not check if code works because believe, logic not problem have.

first of all, php has different ways work scope. inside function addmatrix, setting value variables function scope. assuming want set values class variables. so, must refer 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 using operator -> access class attribute instead of ".". thats problem in code. operator "." used concatenate string. access class attributes or functions use ->. so, check out how 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, rid of $ character after use ->. correct. use $ variable. never when accessing object attributes.

another mistake...php not use datatype variables. objects references already. so, & operator unnecessary. so, lets finish 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;         }  

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -