Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-29-2001, 08:27 PM   #1
MITotaku
Guest
 
Posts: n/a
Advanced PHP tutorials

Where can I find some PHP tutorials for more advanced users?
  Reply With Quote
Old 05-30-2001, 05:31 PM   #2
MITotaku
Guest
 
Posts: n/a
Well will someone at least help me with understanding this code. i suck at OOP.

<?class Cart {
var $items;
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
}
} ?>

Inheriting a class with a Constructor
<?class NamedCart extends Cart {
var $owner;
function NamedCart($name) {
$this->owner = $name;
}
} ?>

Invocation
<? $cart = new NamedCart("PenguinGear");
$cart->add_item(170923, 2);?>

Last edited by MITotaku; 05-30-2001 at 05:34 PM.
  Reply With Quote
Old 05-30-2001, 08:48 PM   #3
MITotaku
Guest
 
Posts: n/a
okay... let me just try and get a yes or no answer.

A variable initialized as an object has a class.
And within that class is a group of functions.
So basically it's a function call, but just using variables to call them???

soooo.... does this make sense???

$object_holding_function->the_function[passed_item]

so if....

$object_holding_function->the_function[passed_item] < 0

... returns true then the function we called that is inside class foo returns a negative number???

I think I finally get it. It's one of those things that I just never got, but now I think I'm close to understanding how it works.
  Reply With Quote
Old 05-30-2001, 08:50 PM   #4
MITotaku
Guest
 
Posts: n/a
Also...

Is it possible to import objects? Like Perl Modules in Perl?
  Reply With Quote
Old 05-30-2001, 11:10 PM   #5
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
Well, as much fun as watching you talk to yourself is, I'll comment a bit.

First off, I've no knowledge of PHP, so the OOP answers are coming from my knowledge of C++/Java/etc.

Your terminology is bad, here's the correct (at least for every object oriented language that I know) terminology:

Variable: An instance of one of the primative types as defined in the language. For example, in c++:
int x = 5;
creates a variable (of type int) named x.

Object: In the same way a variable is an instance of a primitive type, an object is an instance of a class. For example, in C++:
MyClass myObject;
creates an object (of type MyClass) named myObject. myObject is not a variable, it is an object.

Class: A class is a "blueprint" for an object; it encapsulates data members and functions that each object of that class will get a copy of. Sometimes class functions (for example, in C++, static functions) may be called from the class itself. Usually, however, a member function of a class is only callable by using an object of(i.e. instantiated from) the class.

The following is C++ code for a simple class, and examples of how it works:

Class definition:

class MyClass{
public:
int myNumber;
static int myStaticNumber;
static int myStaticFunction(){return myStaticNumber;}
int myMemberFunction(){return myNumber;}
};


Examples of good and bad usage:

MyClass myObject;

myObject.myNumber = 5; //this is OK, it sets myObject's copy of myNumber to 5.
myObject.myStaticNumber = 6; // also OK, this sets THE copy of MyClass::myStaticNumber to 6.
int x = myObject.myMemberFunction(); // OK, this returns myObject.myNumber.
int y = myObject.myStaticFunction(); // OK, this returns MyClass::myStaticNumber.

MyClass::myNumber = 5; // Error -- you cannot use nonstatic data or functions without using an instance (an object) to access the data
MyClass::myStaticNumber = 6; // Allowable, static data may be accessed from the class.
MyClass::myMemberFunction(); // Error -- nonstatic functions/data are only defined for objects
MyClass::myStaticFunction(); // Allowed


Hope this helps.
__________________
Paul M. Victorey
------------------
I am not responsible for any problems that may arise as a result of following my advice. This includes, but is not limited to, computer failure, loss of data, nuclear war, famine, boils, no clean laundry, your daughter running off with a biker gang, or armageddon. Take my advice at your own risk.
Paul Victorey is offline   Reply With Quote
Old 05-31-2001, 06:47 AM   #6
MITotaku
Guest
 
Posts: n/a
Well, when it comes to learning stuff by mself (which I have to because programming class doesn't teach me crap) I can't seem to grasp every little concept. I can only interpret it as best I can... which isn't very good :P.

---------------

If you don't specify a function within the class, it uses the constructor right? So what happens if I don't have a constructor? Or should you always have a constructor to begin with?
  Reply With Quote
Old 05-31-2001, 12:28 PM   #7
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
OK, an object (an instance of a class) in itself can't do a whole lot. It is really a way to group data and functions together.

For example say you had class Car. Car might have the following functions:

void Drive(int distance) // drive the car a distance
int Odometer() //tell what the odometer is set at.

Now, a constructor is a special type of function. A constructor is called only once, when an object is instantiated. Constructors are primarily used to initialize data or allocate memory. Destructors are called when the object is destroyed, and are primarily used to deallocate memory. However, either function can contain any action that you want to happen when the object is created or destroyed.

For example, think of Car, as above. Car might look like this:

class Car{
private:
int odom; //stores the odometer value

public:
Car(){odom = 0;} //constructor initializes our private variable
void Drive(int distance){odom += abs(distance);}
int Odometer(){return odom;}
};

So here, when we "build" (instantiate) the object of type Car, the odometer is set to zero.

So, here are ways to use an object of class Car, legally and illegally:


Car myBuick; //instantiate a Car object

myBuick.Drive(10); // drive 10 miles
myBuick.odom = 0; // can't access the private variable, error.
Car myFord = myBuick; // illegal -- we haven't defined an overloaded operator, so this can't be done.

CarCrusher::Crush(myBuick); // legal, if CarCrusher::Crush() is static and takes an argument of type Car.
Paul Victorey is offline   Reply With Quote
Old 05-31-2001, 02:52 PM   #8
MITotaku
Guest
 
Posts: n/a
What will happen if I don't use a deconstructor? Will it continue to exist in memory? *confused*

-----------

Okay, this would be for someone with a little PHP knowledge:
How can I import code from a PHP script like Perl Modules Is there such thing as a PHP module?

Should I always use the XML format (<%php //code %>)?

Also...

$ptrfoo = &$foo; // is a pointer to foo?
echo $ptrfoo; //prints value address???

$foo = "cool"; //store a value in $foo
$$foo = " dude"; //initialize $cool through reference of $foo?
$foo.$cool == "cool"." dude"; //returns true?
  Reply With Quote
Old 05-31-2001, 04:38 PM   #9
Member (12 bit)
 
Paul Victorey's Avatar
 
Join Date: Mar 1999
Location: MN or WI
Posts: 3,017
In C++, if you don't explicitly create a destructor it only deallocates the space needed for the object itself, and it does destroy the object. But, if, in the constructor, you allocated memory explicitly (via new, for example) you must make a destructor, and deallocate that memory (via delete, for example). If you fail to deallocate memory, you create a memory leak, and this is not a good idea.
Paul Victorey is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:23 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2