0

Hi how can I make a class object with the possibility of doing this:

<?php
    $someClass = new SomeClass;
    $sum = $someClass->addValues(1,22)->sumValues();
    echo $sum; // to give me 23
?>

Sorry for asking! This is what I meant and I just got the idea, so - Sorry for the Post.

<?php 
    class SomeClass {
        private $values = array();
        public function addValue(){
            $this->values = func_get_args();
            return $this;
        }

        public function getSum(){
            $sum = array_sum($this->values);
            return $sum;
        }

    }

    $SomeClass = new SomeClass;
    $result = $SomeClass->addValue(1,22,44,51)->getSum();
    echo $result;
?>

And for all of those who answered - Sorry but this was only an EXAMPLE, so I asked How to do it to help me not to argue with me is it an overkill or not. I needed the way to do it. Not like I will use the same code.

sectus
  • 15,605
  • 5
  • 55
  • 97
Griminvain
  • 95
  • 1
  • 10
  • 4
    If you care more about learning how to code classes rather than just adding numbers together, you can start by checking out the [documentation](http://php.net/manual/en/language.oop5.php) on Classes and object oriented programming in PHP. Otherwise, you would be better off implementing this in another way as others have mentioned. – JCOC611 Oct 07 '14 at 03:41
  • What's the point of addValues if sum values adds the values? lol –  Oct 07 '14 at 03:41
  • Why on earth are you using a class for this? You can just do it like this: `array_sum([1,22]);`. It is overkill using a class for this. – Sverri M. Olsen Oct 07 '14 at 03:42
  • 2
    I believe this is what you're after (the hint was in the title *"Continued Functions"*) ~ [Fluent Interface](http://en.wikipedia.org/wiki/Fluent_interface#PHP) – Phil Oct 07 '14 at 03:53
  • Yes El Yobo and Phil were the only two who guessed what I was searching for. [Sorry for dupplicate, English isn't my original language, so it's not very easy to get the right words and to search in the other articles. ] Thank You Both ElYobo and Phil. For the others when commenting just try to help not to argue !! – Griminvain Oct 07 '14 at 04:15

2 Answers2

1

The term you are finding is "Method chaining", see this link on wikipedia

Basically you need to write a method that returns $this

<?php
    class Foo{
        protected $message;
        function a($foo){
            $this->message .= $foo;
            return $this;
        }
        function b($foo){
            $this->message .= $foo;
            return $this;
        }
        function print_message(){
            echo $this->message;
        }
    }

    $foo = new Foo()
    $foo->a("Hello")->b("World");
    $foo->print_message();
    //output: 'HelloWorld'

?>
rogelio
  • 1,541
  • 15
  • 19
  • Yea i got it sry for the stupid question. I've posted it and did exactly the same thing in my website :D about 2 minutes after i posted it :X – Griminvain Oct 07 '14 at 04:26
0

If you just want to sum a list of numbers then it can easily be done like this:

$sum = array_sum(array(1, 22));

If you are trying to learn how to use classes/objects then you can implement that like this:

class SomeClass {
    protected $values = array();
    public function addValues() {
        $this->values = array_merge($this->values, func_get_args());
        return $this;
    }
    public function sum() {
        return array_sum($this->values);
    }
}
$someClass = new SomeClass;
echo $someClass->addValues(1, 22)->sum(); // 23
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • Omg FAR FAR Away from what I wanted. And even if it wasn't ..I'm not "trying to learn" how to use classes lol xD i already know everything that i have to know about php [after all I am programming in php from 8 years ;D]. So next time please read carefully WHAT the user WANT before type any answer. – Griminvain Oct 07 '14 at 04:24
  • yea now when you edited it its ok ;] but i got it long time ago ;] – Griminvain Oct 09 '14 at 18:05