PRM
PRM

Reputation: 51

How can i change the value of $number here

I want to create a class. Each time it will be called it will increase the value of $number by 1. When it will reach 7, it should return a message "Maximum limit reached". Where to define the $number and how to store the new value in it.

class addClass
{
    public $number = 0;
    public static function addOne($number)
    {
        $number = $number++;
        if ($number == 7) {
            return 'This is 7';
        }
    }
}`

Upvotes: 0

Views: 59

Answers (3)

danielperaza
danielperaza

Reputation: 412

Please bear in mind:

1) The $number parameter in the addOne() method is taking precedence over the $number member in the addClass() parameter.

2) The sentence $number = $number++ is no affecting the variable $number at all, because it is first being assigned.

3) The addOne() method doesn't need to be static, unless is intended to be used without an instance of the class addClass.

4) Static variables only need to be initialize once, refer to the php manual for more information on the static keyword: http://php.net/manual/en/language.oop5.static.php

5) You cannot reference member variables inside a static method (e.g. using $this), because static methods have "class scope" and they are meant to be used without any instance of such class. On the other hand, non static methods require an instance of the class and they can reference members of the class by using $this.

6) Here's an example of how you can do this:

<?php
class addClass{ 
    public function addOne($number) {
        static $limit = 0;

        if (!isset($limit)) {
            $limit = $number;
        } 

        if ($limit+1 == 7) {
            return "Maximum limit reached";
        } else { 
            $limit = $number+1;
        }
    }
}

$a = new addClass();
for($i = 0; $i< 7; $i++) {
    echo $i+1, " => ", $a-> addOne($i), PHP_EOL;
}

Upvotes: 0

Akshay Khale
Akshay Khale

Reputation: 8371

You won't need to have $number in the addOne Function

There are two alternatives

If you don't want to keep $number as static then you can change addOne to a non-static method and access using $this->

class addClass
{
    public $number = 0;
    public function addOne()
    {
        $this->number = $this->number + 1;
        if ($this->number == 7) {
            return 'This is 7';
        }
    }
}

Or if you want addOne to be static then you can declare $number as static and access using self::

class addClass
{
    private static $number = 0;
    public static function addOne()
    {
        self::number = self::number + 1;
        if (self::number == 7) {
            return 'This is 7';
        }
    }
}

Upvotes: 1

Robert
Robert

Reputation: 10390

I think this is what you are looking for based on your description:

class MyNumber {
    private static $number = 0;

    public static function addOne() {
        self::$number++;

        if (self::$number === 7) {
            return 'Maximum limit reached';
        }
        return self::$number;
    }
}

$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();
$result = MyNumber::addOne();

First result is 1

Second result is 2

Third result is 3

Fourth result is 4

Fifth result is 5

Sixth result is 6

Seventh result is Maximum limit reached

Upvotes: 2

Related Questions