LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-12-2009, 10:21 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 32
How to add up integer members of several objects (PHP)?


Hello,
I have a class with lots of members - some of them are plain numbers, some of them are arrays (multidimensional), some of them are other classes (some containing even more classes). So this is kinda rich class. I have several objects of it. I want to have all the properties of these objects summed in another object of the same type.
Here is a faint example of what I want:
Code:
class Foo {
 public $a, $b, $c = array('c1' => 0, 'c2' => 0, 'c3' => 0);
}
class Bar {
 public $d, $e = array(array()), $f = new Foo();
}
$obj1 = new Bar();
$obj1->f->a = 1;
// fill some data
$obj2 = new Bar();
$obj2->f->a = 5;
// fill some data
$obj3 = $obj1 + $obj2;
// at this point have $obj3->f->a = 6;
These are more complex structures with about 30-40 numbers, so I'm trying to avoid writing the addition of every member. Is there a PHP way to do this without much pain?

Last edited by ivanatora; 10-12-2009 at 10:23 AM.
 
Old 10-13-2009, 02:25 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It looks like you are looking for operator overloading which is not supported by PHP. So an alternative approach would be to add a method in your class addMembers() that will perform this for you.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Having objects interact in C++? PoopLoops Programming 25 05-28-2009 11:25 PM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
shell command to add two non integer hexadecimal value ayaskant16 Linux - Kernel 3 11-19-2008 03:59 AM
Objects in C?? kickzha Programming 6 06-17-2006 08:38 PM
dynamically add objects in JavaScript djgerbavore Programming 1 03-11-2005 09:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration