Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-28-2004, 12:09 PM   #1 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 228
falsepride is on a distinguished road
classes?

whats the purpose of a class in php? what is a class? can explain classes to me in a easy to understand way? what does -> do in a class?
falsepride is offline   Reply With Quote
Old 12-28-2004, 12:30 PM   #2 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 228
falsepride is on a distinguished road
okay i think im starting to grasp this. a class is something to control certian variables. like in html when you create a style using .name {background-color:#000000;} any thing you give that class will act in the same way.
falsepride is offline   Reply With Quote
Old 12-28-2004, 06:20 PM   #3 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Sortof.

Your class is a container for functions and variables. Makes it easy to share code and compartmentalize functions related to a common task.

-> in a class points to a variable or function..

And as luck would have it, there is a codenewbie article on OO PHP.

Note: The article seems to go with php4. PHP5 introduces a slew of new stuff.

-r
idx is offline   Reply With Quote
Old 12-28-2004, 06:57 PM   #4 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 228
falsepride is on a distinguished road
im asuming variables within a class or only local, right? i understand classes now, i dont think ill ever use them though.
falsepride is offline   Reply With Quote
Old 12-29-2004, 08:19 AM   #5 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
They are only available within a class. So you'd have to reference the instance of the object you created.

eg:
PHP Code:
$article = new Article(4);
echo 
$article->text
Classes are very powerful if you use them correctly, so I wouldn't ignore them alltogether. Although depending on what types of programs you're developing, you may not [yet] see the need.

-r
idx is offline   Reply With Quote
Old 12-29-2004, 09:20 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,532
sde is on a distinguished road
another cool thing about classes is that PHP supports inheritence too, which means you can use existing classes to add to your own with the keyword extends.
PHP Code:
<?
class person{
  var 
$first_name;
  var 
$last_name;
  
  
// constructor
  
function person($first_name,$last_name){
    
$this->first_name $first_name;
    
$this->last_name $last_name;
  }
}

class 
user extends person{
  var 
$username;
  var 
$password;
  
  
// the person constructor does not execute unless you
  // call it in this extended class. $this->person()
  // we could call it in the user constructor, but i'm
  // just creating its own.
  
  
function user($first_name,$last_name,$username,$password){
    
$this->first_name $first_name;
    
$this->last_name $last_name;
    
$this->user_name $username;
    
$this->password $password;
  }
}

// now when i create a new user, it has all the functions
// that the person class has along with new ones we create.
$myobject = new user("john","smith","jsmith","xx123xx");

echo 
"Welcome "$myobject->first_name ." "$myobject->last_name .";
?>
there is no example here that is too complicated to easily imagine doing it without classes, but it is a powerful tool when you need it.
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
classes and servlets .. oh my sde Java 3 04-27-2004 09:00 AM
What to use for Programming a gaming engine? L06@N Standard C, C++ 30 01-10-2004 11:04 PM
DLLs and Classes moremonks Standard C, C++ 6 07-13-2003 01:27 AM
Anonymous Inner Classes Mjolnir Java 0 05-03-2003 09:26 AM


All times are GMT -8. The time now is 06:55 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting