Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Location
    Chicago, IL
    Posts
    169
    Thanks
    26
    Thanked 3 Times in 3 Posts

    Simple problem with OO PHP

    I am just starting to write a blackjack program. I have plenty of experience with OOP with languages like C++ and Java. However, this is the first time I've attempted it in PHP. The error I am getting is "Fatal error: Call to a member function getNumber() on a non-object". Obviously, it's because for some reason, my card object is not being created and/or assigned to the deck appropriately.

    Here's card.php:
    PHP Code:
    <?php
    class card
    {
        private 
    $suite;
        private 
    $number;
        
        function 
    _constructor($suite$number)
        {
            
    $this->suite $suite;
            
    $this->number $number;
        }
        
        public function 
    getSuite()
        {
            return 
    $this->suite;    
        }
        
        public function 
    getNumber()
        {
            return 
    $this->number;    
        }

    }
    ?>
    and deck.php
    PHP Code:
    <?php
    include("card.php");
    class 
    deck
    {
        public 
    $deck = array();
        
        function 
    _constructor()
        {
            
    $suiteArr = array("H","D","C","S");
            
            for(
    $i=0;$i<52;$i++)
            {
                
    $cardNumber = (int)($i+1);
                
    $this->deck[$i] = new card($suiteArr[$i 4],$cardNumber);
            }
        }
        
        public function 
    shuffleDeck()
        {
            
    shuffle($this->deck);
        }
    }

    $gameDeck = new deck;
    for(
    $i=0;$i<52;$i++)
    {
        echo 
    $gameDeck->deck[$i]->getNumber() . $gameDeck->deck[$i]->getSuite() . " ";
    }
    ?>
    The error, I believe, is on the line $this->deck[$i] = new card($suiteArr[$i % 4],$cardNumber);

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Your deck and card constructors should be
    function __construct()
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    wldrumstcs (01-09-2010)

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Also, I know it doesnt change the code, but it's suit not suite
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    Regular Coder
    Join Date
    Jul 2009
    Location
    Chicago, IL
    Posts
    169
    Thanks
    26
    Thanked 3 Times in 3 Posts
    Hahaha I didn't even think about that. Thanks a lot!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •