473,626 Members | 3,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes, don't follow.

Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great with a class. I
just don't grasp the whole concept, and how to do it.

I want to make a Collectable Card Game Draft Engine...(if any of you
play VS System, LOTR, Magic: The Gathering, you know what I am talking
about.)

It would be way to complicated for just typing in regular functions and
calling em.

So this is what I've seen That I don't understand.

Say i have a database with cards that are common(c), uncommon(u),
rare(r) from 3 sets alpha(a), beta(b), unlimited(u).

I need to 1st create 24 packs that contain 11commons, 3uncommons, and
1rare each.

Bam thats a class.

would it look like this?
Class CreateDraft{
var $set;
var $rarity;
function SelectRandomCar d{
//connect to database in file header
//
$querycardbase = "SELECT * FROM `mtg_base` WHERE rarity = $rarity
AND set = $set LIMIT $rarity_num";//rarity and rarity_num are related
c=11,u=3,r=1, set dictates cards available.
$querycardbase_ result6 = @mysql_query ($querycardbase );
while ($cardbaserow = @mysql_fetch_ar ray ($querycardbase _result)) {

$packarr = $cardbaserow[card_id]
}
function InsertPack{
$insertpack = "SQL STATEMENT INSERTING AN ARRAY";//I know, i know
its late and I just want to understand.
}
}

I guess I am asking for help.. Because I do not understand.

Oct 5 '05 #1
16 2183
First, PHP4 and PHP5 have different implementations of object oriented
functionality. Being an optimist, I am going to assume you use PHP5.
Please correct me if you are using PHP4.

What I see in your story is:
- You have sets and packs of cards. This may be just an array of card
objects, or a collection class.
- You have a "generic" card object and special implementations of it.
This suggests that the generic card is abstract, and the special(common,
uncommon, rare) cards are extensions to that. But only if they BEHAVE
differently (have extra methods or different implementations of some
methods).

Best regards

Jace Benson wrote:
Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great with a class. I
just don't grasp the whole concept, and how to do it.

I want to make a Collectable Card Game Draft Engine...(if any of you
play VS System, LOTR, Magic: The Gathering, you know what I am talking
about.)

It would be way to complicated for just typing in regular functions and
calling em.

So this is what I've seen That I don't understand.

Say i have a database with cards that are common(c), uncommon(u),
rare(r) from 3 sets alpha(a), beta(b), unlimited(u).

I need to 1st create 24 packs that contain 11commons, 3uncommons, and
1rare each.

Bam thats a class.

would it look like this?
Class CreateDraft{
var $set;
var $rarity;
function SelectRandomCar d{
//connect to database in file header
//
$querycardbase = "SELECT * FROM `mtg_base` WHERE rarity = $rarity
AND set = $set LIMIT $rarity_num";//rarity and rarity_num are related
c=11,u=3,r=1, set dictates cards available.
$querycardbase_ result6 = @mysql_query ($querycardbase );
while ($cardbaserow = @mysql_fetch_ar ray ($querycardbase _result)) {

$packarr = $cardbaserow[card_id]
}
function InsertPack{
$insertpack = "SQL STATEMENT INSERTING AN ARRAY";//I know, i know
its late and I just want to understand.
}
}

I guess I am asking for help.. Because I do not understand.

Oct 5 '05 #2
You're not exactly explaining what the problem is, and I have not a lot
of experience with PHP classes, but at first glance the
SelectRandomCar d function is not so random. It will always return the
same decks for a given $set. Besides, your $packarr variable will
probably not work as an array, more like a simple variable being
overwritten every time through the loop.

Hope that helps. Anyway, it would help if you could point us at more
specific problems :-)

Oct 5 '05 #3
Jace Benson wrote:
Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great with a class. I
just don't grasp the whole concept, and how to do it.


Think of a class as a container for data and methods (functions)
which operate on that data. Here's something that might look like
classes for a deck of cards:

<?php

class Card {
var $suit;
var $value;

function Card() {

// somehow initializes a card
$this->suit = "spades";
$this->value = "10";
}

function display() {
echo "Hello, I am a " . $this->value . " of " . $this->suit ."\n";
}
}
class Deck {
var $cards = array(); // cards are stored here
var $size = 52; // 52 normal, 5
var $jokers = false; // boolean: if yes, adds 2 to deck size

function Deck() {

if ($this->jokers) {
$this->size += 2;
}

// eg. add "empty" cards to deck
for ($i=0; $i < $this->size; $i++) {
$this->cards[] =& new Card();
}
}

function shuffle() {
// some kind of function to reshuffle
$this->cards = array_reverse($ this->cards);
}

function display() {
// displays the entire deck
foreach ($this->cards as $card) {
$card->display();
}
}
}
// Ok, so now that we have defined our classes, we can play some cards:

$d = new Deck();
$d->shuffle();
$d->display();
?>

And our script will output (in case above), 52 lines of: "Hello, I am
a 10 of spades".

/Marcin
Oct 5 '05 #4
Jace Benson wrote:
Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great with a class. I
just don't grasp the whole concept, and how to do it.

I want to make a Collectable Card Game Draft Engine...(if any of you
play VS System, LOTR, Magic: The Gathering, you know what I am talking
about.)

It would be way to complicated for just typing in regular functions and
calling em.

So this is what I've seen That I don't understand.

Say i have a database with cards that are common(c), uncommon(u),
rare(r) from 3 sets alpha(a), beta(b), unlimited(u).

I need to 1st create 24 packs that contain 11commons, 3uncommons, and
1rare each.

Bam thats a class.

would it look like this?
Class CreateDraft{
var $set;
var $rarity;
function SelectRandomCar d{
//connect to database in file header
//
$querycardbase = "SELECT * FROM `mtg_base` WHERE rarity = $rarity
AND set = $set LIMIT $rarity_num";//rarity and rarity_num are related
c=11,u=3,r=1, set dictates cards available.
$querycardbase_ result6 = @mysql_query ($querycardbase );
while ($cardbaserow = @mysql_fetch_ar ray ($querycardbase _result)) {

$packarr = $cardbaserow[card_id]
}
function InsertPack{
$insertpack = "SQL STATEMENT INSERTING AN ARRAY";//I know, i know
its late and I just want to understand.
}
}

I guess I am asking for help.. Because I do not understand.


I don't understand in detail what you're trying to do, but if you have a
class called 'Create<anythin g>', the chances are you've not understood
classes.

A class (or rather, an object of a particular class) represents a thing
- in your application probably a pack of cards, maybe an individual
card, maybe a set (alpha, beta), maybe a rarity (though that might be a
single value, in which case there's probably no point in having a class
for it).

Create would then be a method (function belonging to a class) - perhaps
the constructor that every class must have, perhaps a different method.

If you declare a function inside a class as you have for
SelectRandomCar d, it is a method, so when it is called it will be called
to operate on a particular object (instance of the class), and you need
to refer to the properties of the particular object with the 'this'
pointer: $this->rarity.

Don't use classes because you think they're cool: use them if you're
prepared to think of your program in terms of creating and operating on
objects (that know how they should be operated on).

I now usually approach any programming question in terms of objects, but
it did take some work to get there!

Colin
Oct 5 '05 #5
Hi Jace,

If you are used to procedural programming OOP can be hard to grasp in
the beginning. The problem is that you see your computer as a single
entity with a single processor and memory space. Your procedural program
is essentially a list of instructions for this processor, referring to
variables in this memory. You need to let go of that idea. Objects are
more like the those beasts/puppets that run across your screen in a
computer game like boulderdash: a whole bunch of entities that seem to
live inside your computer, each doing its own things, remembering for
itself what happend and what it is doing in its own private memory
space, and interacting with one another.

With OOP, objects are things you can call methods on. If you call a
method on an object, the object may do somthing. It may also remember
somthing. What is does may depend on the things it has previously
remembered. It is like a little home computer: you can type a command,
it will print a reaction (or trigger an error if it does not know the
command). You don't need to know how it works internally. You just need
to know what will happen for each command.

But there's more. Objects are networked. When you call a method on one
object, it may ask other objects to do things by calling more methods on
them. If you call a function in procedural programming, you know what
code will be executed. If you call a method on an object, you don't,
unless you know what object you are calling the method on. But for
making the method call you will probably use a variable in which you
have put a reference to the object. So if at some point your code puts a
different object in that variable, all method calls on that object may
end up executing different code... Yes, object references are much like
function pointers. Those objects can hold other objects in their member
variables and make more method calls on those objects, and so on. This
easily leads to a magnitude of spaghetty you can not even dream of with
procedural code, even if you are using GOTO's! This spaghetty even
changes dynamicly as the program changes the contents of variables! New
objects may be created. References to objects may be returned and stored
.. AARCH!

This is what classes are made for: To create some structure in the
object spaghetty. Firs we classify all objects. Then we only define
methods in the classes. So if you have a variable referencing an object,
and you know the class of the object in advance, you know what code will
be executed if you call a method on that object. Becuase most
programmers somehow tend to know the type (class with subclasses) of
what is in each variable, they can find their way in the spaghetty.
Formally it's still very messy, but who cares, programming IS a matter
of psychology after all ;-)

Greetings,

Henk Verhoeven,
www.phpPeanuts.org.

Jace Benson wrote:
Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great with a class. I
just don't grasp the whole concept, and how to do it.

I want to make a Collectable Card Game Draft Engine...(if any of you
play VS System, LOTR, Magic: The Gathering, you know what I am talking
about.)

It would be way to complicated for just typing in regular functions and
calling em.

So this is what I've seen That I don't understand.

Say i have a database with cards that are common(c), uncommon(u),
rare(r) from 3 sets alpha(a), beta(b), unlimited(u).

I need to 1st create 24 packs that contain 11commons, 3uncommons, and
1rare each.

Bam thats a class.

would it look like this?
Class CreateDraft{
var $set;
var $rarity;
function SelectRandomCar d{
//connect to database in file header
//
$querycardbase = "SELECT * FROM `mtg_base` WHERE rarity = $rarity
AND set = $set LIMIT $rarity_num";//rarity and rarity_num are related
c=11,u=3,r=1, set dictates cards available.
$querycardbase_ result6 = @mysql_query ($querycardbase );
while ($cardbaserow = @mysql_fetch_ar ray ($querycardbase _result)) {

$packarr = $cardbaserow[card_id]
}
function InsertPack{
$insertpack = "SQL STATEMENT INSERTING AN ARRAY";//I know, i know
its late and I just want to understand.
}
}

I guess I am asking for help.. Because I do not understand.

Oct 7 '05 #6
(I posted this also a few months ago in the XP yahoo group) When I
explain object-oriented programming, I usually draw the parallel
to electronic devices.

When I was young, I had a tape recorder dating from about 1965. It came
with a _huge_ electrical scheme that contained everything. You could
look at it for hours and still discover new parts of that same tape
recorder. For programmers, it looked a lot like an old BASIC program.

We don't build tape recorders like that anymore. Factories make not one
tape recorder, but a whole line of them. With different size, colour,
power, with an equalizer, with extra engine speed, etc.
If you open a modern tape recorder, you see that it is built up of
sub-assemblies that are plugged together. You can easily change the tone
control for an equalizer, or repair the left preamplifier by simply
replacing it for a new one.
These are, for me, objects. They are separately testable (with unit
tests), allow reuse (the line of tape recorders, but the preamplifier is
also used in an MP3-player) and have a small task.
A plug is the object's interface. Circumvening the plug (exposing
internal details or using global variables or singletons) is the same as
hard-wiring a point on one circuit board to another. You can even SEE it
is bad in electronics, because hard-wired objects are not separate
anymore, cannot be separately tested, etc.

Best regards
Oct 8 '05 #7
Well... I am trying to understand here. Before I start, thanks for all
the information you've given me.

So please correct me if I am wrong.

Classes are a container.. if you will something to put what a <thing>
does.
if for instance I was making a program on a pet. I would want to label
it Class Pet {...}
(dont be so tight on the gramatical errors, I am just trying to figure
this out).. Anyways In the class Pet I would put in it the
methods?(these are also the functions)? that the pet would do. So say
I wanted to call the class pet and make a new instance of a dog and i
wanted that dog to poop.
Would this be how i should look at that in OO programming?
//make this class
//classes dont create things methods do
Class Pet {
var $pets_name;
function ChooseAPet() {
//some function pulling data from an array printing
//it to a form to select it and send it back with a name
}
}
Class Action {
var $action;
function Action(){
//this will be called whenever the class is caled
//some thing like before but pulling data for what an animal can
do.
}
function Stop(){
//something to stop an action for instance
//a pet might take a long time to sleep but not long to jump
}
function Create(){
//when some actions happen things are created like stools
}
}

not the best example but this would be a valid theroized Class
design..?

I feel like i dont' get it. I under stand to seperate is good, but I
am lost.

Oct 11 '05 #8
In general, OOP is more or less what your example shows. The class
defines what the object is and can do, while the object actually *is*
something (an instance of the class) and *does* something. See if this
pseudocode helps...
Class Dog
{
var race;
var food_it_likes;

function Bark
{
....
}

function Poop
{
....
}

} //End Dog Class
//You have just defined what a dog is and can do, so that you can have
multiple dogs doing stuff. Then, in your program...

Dog Vincent;
Dog Toby;

Toby.race = "Scottish Terrier";
Toby.food_it_li kes = "Tasty Chicken";
Vincent.race = "Chiuaua";
Vincent.food_it _likes = "Human Flesh";

Vincent.Bark;
Toby.Poop;

//You now *actually* have two dogs who can bark and poop. One is
barking (Vincent the scary assasin little chiuaua), and the other is
pooping (Toby the elegant black Scottish).

Greetings

Oct 11 '05 #9
Jace Benson wrote:
Well... I am trying to understand here. Before I start, thanks for all
the information you've given me.

So please correct me if I am wrong.

Classes are a container.. if you will something to put what a <thing>
does.
if for instance I was making a program on a pet. I would want to label
it Class Pet {...}
(dont be so tight on the gramatical errors, I am just trying to figure
this out).. Anyways In the class Pet I would put in it the
methods?(these are also the functions)? that the pet would do. So say
I wanted to call the class pet and make a new instance of a dog and i
wanted that dog to poop.
Would this be how i should look at that in OO programming?
//make this class
//classes dont create things methods do
Class Pet {
var $pets_name;
function ChooseAPet() {
//some function pulling data from an array printing
//it to a form to select it and send it back with a name
}
}
Class Action {
var $action;
function Action(){
//this will be called whenever the class is caled
//some thing like before but pulling data for what an animal can
do.
}
function Stop(){
//something to stop an action for instance
//a pet might take a long time to sleep but not long to jump
}
function Create(){
//when some actions happen things are created like stools
}
}

not the best example but this would be a valid theroized Class
design..?

I feel like i dont' get it. I under stand to seperate is good, but I
am lost.


Hi, Jace,

Samuel has given you an excellent example. I'll try another (in a sec.).

The whole idea of object oriented programming is to bring "real world"
objects into the "programmin g world". That is, to create something in
the programming world which mimics what we're already familiar with.

If you had a real world pet, would you think of it as performing
"Action", "Stop" and "Create"? Or would you think of it as performing
"Bark", "Eat", "Sit", "Poop"?

The other example I've used in a lot of classes - an automobile. Take
some basic actions - start, stop, change gear, accelerate and stop.

The car itself keeps track of basic things like its running status, the
gear it's in, its speed, and how much gas is in the tank.

When you "start" the car, some things must be in place. The car must
not already be started, there must be gas in the tank and the gear must
be in park or neutral (assuming an automatic transmission).

To accelerate, the engine must be running, the brake off and the
transmission in gear.

The beauty here is - you, as a user of the car class, don't have to
worry about testing the various states - the car class will do it for
you (for instance - lets say you add a new status "Battery Charged").
You change the "Start" method to test for this - but don't need to
change anything else in your program.

OO programming is a completely different way of thinking. Interestingly
enough, in my classes I've found the people with the least programming
experience typically have the least trouble making the switch. Those
with lots of programming experience have the most trouble. It's quite
difficult to "unlearn" years of learning!
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Oct 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
8732
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming aspect, but are there any advantages to having classes as opposed to just functions? I've heard that the classes are better because they only load the functions they need to, where with an include file of functions, the whole page gets loaded, even if...
6
1778
by: beliavsky | last post by:
I have started using classes with Python and have a question about their use. In Python, 'attributes are all "public" and "virtual" in C++ terms; they're all accessible everywhere and all looked up dynamically at runtime' (Quoting "Learning Python", 2nd. ed., p367). It seems to me that two good conventions are to (1) initialize all attributes in the __init__ function (2) avoid creating new attributes elsewhere that are not initialized...
7
1801
by: verbatime | last post by:
Please explain me how this works - or should work: Got my two classes - bcBasic (baseclass) and the derived cBasic. //--------------------------------------- class bcBasic { int number; virtual long myfunc(void); }
45
3593
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
11
1935
by: mem | last post by:
Concrete classes are ones that can be instantiated directly, as long as it doesn't have pure virtual function. However, people suggest that "Don't derive from concrete classes." Does this mean that either make it abstract base class for it to be derivable or keep them as "concrete" not to be derived at all? Then what's the purpose of "concrete classes" of ordinary virtual function(s)? Thanks!
6
2933
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by calling Mesh class functions. Let's say they point to each other like this: class Vertex { HalfEdge *edge; }; class HalfEdge { Vertex* vert;
25
1488
by: Brian | last post by:
Can some one please tell me what I'm doing wrong. I'm trying to create a class called Dog, but Visual Basic tells me that I can't enter Wolf.age....why is this? Public Class Form1 Public Class DOG Dim COLOUR As String Dim AGE As Integer Dim NAME As String
7
1101
by: TClancey | last post by:
Hi all. I'm having some problems with a project I'm working on. I have a Class Library which contains several Component Classes, each working independantly. It seems that I can't have two Component Classes that Inherit the same standard vb control, ie PictureBox. Do I have to create a whole seperate Class Library for each Component Class? I've tried creating a new Class Library within the same application but get
4
1860
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since interface classes cannot be directly instantiated, yet serve as virtual base classes for implementations, the constructors should take no arguments and should be listed as protected. Also, for similar
12
11058
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.