...

View Full Version : Simple problem with OO PHP



wldrumstcs
01-09-2010, 05:47 PM
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
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
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);

JAY6390
01-09-2010, 06:09 PM
Your deck and card constructors should be
function __construct()

JAY6390
01-09-2010, 06:10 PM
Also, I know it doesnt change the code, but it's suit not suite :)

wldrumstcs
01-09-2010, 08:58 PM
Hahaha I didn't even think about that. Thanks a lot!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum