Page 1 of 1

can't get property

Posted: 01 August 2020, 22:29
by Ginso
Hello, i can't find my error, maybe some here can help:
I have the following class

Code: Select all

class Card extends APP_GameClass
{

	public function __construct(){
	}

	public $id;
	public $name;
	public $text;
	public $copies = [];
	public $implemented = false;
	public $type; // see dbmodel.sql
	public $color;
	public $effect; // array with type, impact and sometimes range

	public function play($player) {
		switch ($this->effect->type) {
		//...
		}
	}

	//...
}
and the following extension

Code: Select all

class CardBang extends Card {
  public function __construct()
  {
    parent::__construct();
    $this->id    = CARD_BANG;
    $this->name  = clienttranslate('BANG!');
    $this->text  = "A Bang to a player in range. Can usually only be played once per turn";
    $this->color = BROWN; //BROWN, BLUE, GREEN
	$this->type  = 10;
    $this->effect = ['type' => BASIC_ATTACK, // BASIC_ATTACK, DRAW, DEFENSIVE, DISCARD, LIFE_POINT_MODIFIER, RANGE_INCREASE, RANGE_DECREASE, OTHER
					'range' => 0,
					'impacts' => INRANGE // NONE, INRANGE, SPECIFIC_RANGE, ALL_OTHER, ALL, ANY
					]; 
    

    
    $this->copies = [
      BASE_GAME => [ 'AS', '8D', '9D', '10D', 'JD', 'QD', 'KD', 'AD', '2C', '3C', 'QH', 'KH', 'AH', '2D', '3D', '4D', '5D', '6D', '7D', '4C', '5C', '6C', '7C', '8C', '9C' ],
      DODGE_CITY => [ '8S', '5C', '6C', 'KC'],
    ];
  }
}
now, i create an instance of CardBang and execute the play method:

Code: Select all

$card = new CardBang();
$card->play($player_id);
Why do i get an error
Trying to get property 'type' of non-object
at the switch line?

Re: can't get property

Posted: 02 August 2020, 01:47
by Victoria_La
$this->effect['type']

Re: can't get property

Posted: 02 August 2020, 10:43
by Ginso
ok thank you, another question if you don't mind:
i added a function to the Card class:

Code: Select all

function askForTarget($targets, $player_id) {
	//...
	self::notifyPlayer(...);
}
and i call this in play function using

Code: Select all

$this->askForTarget($player_ids, $player);
but i get an error:
Call to undefined method BangCard::notifyPlayer()
Why can i use db function using self but not this one and how would i do it?

Re: can't get property

Posted: 02 August 2020, 12:04
by Tisaac
In other project, I usually pass the game object to the construct of other class so that you can then use

Code: Select all

$this->game->notifyPlayer(...)
Just look at the BangCharacter class I've already put in the repo to see how to construct the class.

Re: can't get property

Posted: 03 August 2020, 14:48
by Victoria_La
I never use self:: in php it only applies to methods of its own class and does not work with overloading, always use
$this-> it applies for everything and you don't need to rewrite the code if you decided to extract a class.