Trouble understanding the calling of global variables with in a class to a method within the class

watkins1179

I am having trouble with my numberOfDecks method. I try to call the array of cards that is created in my constructor, but the an error keeps coming up that I am passing an undefined variable ($cards). I thought since it is a global variable $cards would be able to be called to the numberOfDecks method.

<?php
/* creating a deck of cards class to be used in a blackJack game*/

class Deck{

  public  $cards = array();

  //creates an instance of a deck of cards (works)

  public function __construct(){
    $values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
    $suits =array('Diamond','Club','Heart','Spade');
      foreach ($suits as $suit) {
         foreach($values as $value){
           $this->cards[] = "$value of $suit's";
         }
      }
  }

  /*trying to add more decks to increase number of total cards
  in my array  (does not work)*/

  public function numberOfDecks($number){

    $this->cards = $cards;
    $this->number= $number;
    for($i = 0 ; $i < $number; $i++){
          array_push($cards[],$i);
    }
    return $cards;

  }
}



$deck = new Deck();//works as expected
$deck->numberOfDecks(3);//trouble 
$shuffled = shuffle($deck->cards);//works as expected
var_dump($deck);
Jeff Puckett

Your variable $cards is undefined because you don't declare it.

public function numberOfDecks($number){

$this->cards = $cards;

You probably meant to flip the assignment direction.

$cards = $this->cards;

Given your comment for adding decks to the object total deck, after flipping the assignment direction, try using array_merge

Here's a working fiddle.

<?php

class Deck{

  public  $cards = array();

  //creates an instance of a deck of cards (works)

  public function __construct(){
    $values =array('2','3','4','5','6','7','8','9','10','J','Q','K','A');
    $suits =array('Diamond','Club','Heart','Spade');
      foreach ($suits as $suit) {
         foreach($values as $value){
           $this->cards[] = "$value of $suit's";
         }
      }
  }

    public function numberOfDecks($number){

        $cards = $this->cards;

        $this->number = $number;
        for($i = 0 ; $i < $number; $i++){
            $this->cards = array_merge($this->cards, $cards);
        }

    }
}



$deck = new Deck();//works as expected

// how many cards are intially constructed?
echo "count of cards in new deck: " . count($deck->cards) . "\n<br/>\n";

// add 3 more decks of cards
$deck->numberOfDecks(3);//trouble 
echo "count of cards after adding 3 decks: " . count($deck->cards);

// $shuffled = shuffle($deck->cards);//works as expected
// var_dump($deck);

Results in this output:

count of cards in new deck: 52
count of cards after adding 3 decks: 208

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a method within the same class

From Dev

Having trouble calling a method from another class

From Dev

Ruby: trouble calling class instance from method

From Dev

Trouble understanding constructors and reasons for calling them in another class

From Dev

Calling function within method python class

From Java

Calling super Method within Anonymous Class

From Dev

Calling a constructor from method within the same class

From Java

OOP: Calling a public method within the same class

From Java

Calling super method from within super class

From Dev

Calling a private method within a class in Perl 6

From Dev

Calling main method within the class python

From Dev

PHP Calling a function within a static method of a class

From Dev

Calling a singleton method within the singleton class in Ruby?

From Dev

Calling an object of a method of a class within another method of the same class

From Dev

Having trouble calling this class

From Java

Calling a method on an Object from within a Class vs from within a method

From Dev

Trouble understanding private attributes in classes and the class property method in Python 3

From Java

Having trouble calling a method to edit a string from another class in java

From Dev

Trouble calling a non-static method from an abstract class

From Dev

Trying to access the instance variables of the class that I am calling a class method on

From Dev

Calling an object within a class

From Dev

Trouble calling methods from a class/

From Java

Java: trouble understanding recursive method calling

From Dev

Global variables in class and outside class

From Dev

NameError: global name not defined when calling method inside class

From Dev

Understanding Python class variables

From Dev

Calling a method from a different class that uses variables outside the method

From Dev

Reference to (module only) global function within a class method

From Dev

Problems calling a method within the same class in f#

Related Related

  1. 1

    Calling a method within the same class

  2. 2

    Having trouble calling a method from another class

  3. 3

    Ruby: trouble calling class instance from method

  4. 4

    Trouble understanding constructors and reasons for calling them in another class

  5. 5

    Calling function within method python class

  6. 6

    Calling super Method within Anonymous Class

  7. 7

    Calling a constructor from method within the same class

  8. 8

    OOP: Calling a public method within the same class

  9. 9

    Calling super method from within super class

  10. 10

    Calling a private method within a class in Perl 6

  11. 11

    Calling main method within the class python

  12. 12

    PHP Calling a function within a static method of a class

  13. 13

    Calling a singleton method within the singleton class in Ruby?

  14. 14

    Calling an object of a method of a class within another method of the same class

  15. 15

    Having trouble calling this class

  16. 16

    Calling a method on an Object from within a Class vs from within a method

  17. 17

    Trouble understanding private attributes in classes and the class property method in Python 3

  18. 18

    Having trouble calling a method to edit a string from another class in java

  19. 19

    Trouble calling a non-static method from an abstract class

  20. 20

    Trying to access the instance variables of the class that I am calling a class method on

  21. 21

    Calling an object within a class

  22. 22

    Trouble calling methods from a class/

  23. 23

    Java: trouble understanding recursive method calling

  24. 24

    Global variables in class and outside class

  25. 25

    NameError: global name not defined when calling method inside class

  26. 26

    Understanding Python class variables

  27. 27

    Calling a method from a different class that uses variables outside the method

  28. 28

    Reference to (module only) global function within a class method

  29. 29

    Problems calling a method within the same class in f#

HotTag

Archive