LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-17-2004, 08:49 AM   #1
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Rep: Reputation: 17
PHP: how to use a method from a class in the same class


Stupid question maybe but it's part of another problem I have.
1) How can I use a private method in the class that owns that method?
self::function(); ??
this->function(); ??
2) can a non-static method change a private static variable from that class?

The goal is:
The class must remember how many instances are made of itself, here I use the static variable.
This static variable is used to assign a unique value (key if you want)

This is the class:

<?php

class card
{
private $Card_owner_id;
private static $Card_total;
private $Card_game_id;

public function __construct()
{
self::createNewCard();
$this->Card_owner_id = 0;
}

private function createNewCard()
{
self::$Card_total++;
$this->Card_game_id = self::$Card_total;
}

public function giveCardGameId()
{
return $this->Card_game_id;
}

public function giveCardTotal()
{
return self::$Card_total;
}
}
?>

tried to execute the following code:

<?php
require 'card.php'
for ($i = 0; $i < 10; $i++) {
$card[$i] = new card();
}
for ($i = 0; $i < 10; $i++) {
print "CardGameId: $card[$i]->giveCardGameId() : $card[$i]->giveCardTotal() <br />";
}
?>

The path to include is correct but I think that I'm missing something crucial.
Maybe it's impossible to change static variables inside a class by a method that is part of the class because then that method can only be called from an instance of that class. And static variables can only be changed from the class itself, not by an instance of that class?
Then only static methods can change static variables?

any help appreciated.
thanks,
Lieven
 
Old 09-17-2004, 09:06 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
1) $this->function();

2) I don't know the use of private in php, do you use php5 ?
try :
PHP Code:
 class card
{
var 
$Card_owner_id;
var 
$Card_total;
var 
$Card_game_id;

function 
card()
{
    
$this->createNewCard();
    
$this->Card_owner_id 0;
    
$this->Card_total 0;
    
$this->Card_game_id=0;
}

function 
createNewCard()
{
    
$this->Card_total++;
    
$this->Card_game_id $this->Card_total;
}

function 
giveCardGameId()
{
    return 
$this->Card_game_id;
}

function 
giveCardTotal()
{
    return 
$this->Card_total;
}
}
?> 

Last edited by Cedrik; 09-17-2004 at 09:08 AM.
 
Old 09-17-2004, 09:18 AM   #3
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 17
I'm indeed using php5 because php4 didn't understand encapsulation like private, protected and public.
The problem with the solution that you present here is that whenever I create a new instance of the card class, that class' variable Card_total will become 0 and thus it won't indicate the total amount of instances created from the card class.
And then it's no use to add that value to the Card_game_id because it won't be unique. All instances will have 1 as Card_game_id instead of an increasing number.

Am I allowed to change static variables with a method of that class? Because I feel like I can't use that class' method before I create an instance of it but then again, the static variable cannot be changed by the instance of the class.

A little bit confusing to me.
thanks,
Lieven
 
Old 09-17-2004, 09:23 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
In a concept view, I don't see why the card object should hold the total cards number, instead I will create another class which could hold all cards in game.
 
Old 09-17-2004, 09:35 AM   #5
ldp
Member
 
Registered: Apr 2004
Location: Belgium Antwerpen
Distribution: slackware - knoppix
Posts: 141

Original Poster
Rep: Reputation: 17
That's true indeed, and there is indeed a class called deck which holds all the cards but for another reason, I would like to have the card class itself to have a unique number. I guess I'll have to pass that number from the deck class to the card class then. Also I was wondering if it could be possible with a static variable.
But I'll do it otherwise then.
thanks for the replies.

Lieven
 
Old 09-17-2004, 09:52 AM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
I can do that with php4 :

PHP Code:
<?php
class card {
    var 
$id;
    function 
card($id)
    {
        
$this->id $id;
    }

    function 
getID()
    {
        return 
$this->id;
    }
}

class 
game {
    var 
$cards;
    function 
game()
    {
        
$this->cards = Array();
        for(
$i 0$i 53$i++) {
            
$this->cards[$i] = new card($i);
        }
    }

    function 
getCard($num)
    {
        
$obj $this->cards[$num];
        return 
$obj->getID();
    }
}

$gameObj = new game();
echo 
$gameObj->getCard(2);
?>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing a vector class from a list class purefan Programming 9 04-14-2005 10:48 PM
Java help (accessing array elemonts from another class or method) Tru_Messiah Programming 6 05-14-2004 09:20 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 08:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 11:52 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM


All times are GMT -5. The time now is 10:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration