Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

PHP Classes

 
   Database Forums (Home) -> PHP RSS
Next:  Updating an INT column with a sequential value?  
Author Message
logankriete

External


Since: Dec 07, 2004
Posts: 1



(Msg. 1) Posted: Tue Dec 07, 2004 9:30 am
Post subject: PHP Classes
Archived from groups: comp>lang>php (more info?)

What is a PHP class? I've seen a lot of talk about these and how they
can help you out, but I've never seen a very good explanation. I've
been working with PHP for just over 6 months and usually hard code
everything into my scripts. Recently I've begun putting frequently-used
things (like my MySQL connect function) in a seperate file and
including them. But I still haven't seen the use of classes or even
functions for that matter. And one more thing - how do you set up a
function to accept variables?

Could someone please help me out?!?!

 >> Stay informed about: PHP Classes 
Back to top
Login to vote
user235

External


Since: Jun 07, 2004
Posts: 37



(Msg. 2) Posted: Tue Dec 07, 2004 10:31 am
Post subject: Re: PHP Classes [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
"Logan K." < > wrote:

 > What is a PHP class? I've seen a lot of talk about these and how they
 > can help you out, but I've never seen a very good explanation. I've
 > been working with PHP for just over 6 months and usually hard code
 > everything into my scripts. Recently I've begun putting frequently-used
 > things (like my MySQL connect function) in a seperate file and
 > including them. But I still haven't seen the use of classes or even
 > functions for that matter. And one more thing - how do you set up a
 > function to accept variables?
 >
 > Could someone please help me out?!?!

Read a book on Object Oriented programming:

<a rel="nofollow" style='text-decoration: none;' href="http://www.sitepoint.com/books/phpant1/" target="_blank">http://www.sitepoint.com/books/phpant1/</a>

--
DeeDee, don't press that button! DeeDee! NO! Dee...

 >> Stay informed about: PHP Classes 
Back to top
Login to vote
Chris Hope

External


Since: Mar 26, 2004
Posts: 93



(Msg. 3) Posted: Tue Dec 07, 2004 2:40 pm
Post subject: Re: PHP Classes [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Michael Vilain wrote:



 >
  >> What is a PHP class? I've seen a lot of talk about these and how they
  >> can help you out, but I've never seen a very good explanation. I've
  >> been working with PHP for just over 6 months and usually hard code
  >> everything into my scripts. Recently I've begun putting
  >> frequently-used things (like my MySQL connect function) in a seperate
  >> file and including them. But I still haven't seen the use of classes
  >> or even functions for that matter. And one more thing - how do you
  >> set up a function to accept variables?
  >>
  >> Could someone please help me out?!?!
 >
 > Read a book on Object Oriented programming:
 >
<font color=purple> > <a rel="nofollow" style='text-decoration: none;' href="http://www.sitepoint.com/books/phpant1/</font" target="_blank">http://www.sitepoint.com/books/phpant1/</font</a>>
 >

And also read the PHP manual

<a rel="nofollow" style='text-decoration: none;' href="http://www.php.net/manual/en/language.oop.php" target="_blank">http://www.php.net/manual/en/language.oop.php</a>
<a rel="nofollow" style='text-decoration: none;' href="http://www.php.net/manual/en/language.oop5.php" target="_blank">http://www.php.net/manual/en/language.oop5.php</a>

--
Chris Hope - The Electric Toolbox - <a rel="nofollow" style='text-decoration: none;' href="http://www.electrictoolbox.com/" target="_blank">http://www.electrictoolbox.com/</a>
 >> Stay informed about: PHP Classes 
Back to top
Login to vote
nospam25

External


Since: Apr 22, 2004
Posts: 10



(Msg. 4) Posted: Sun Dec 19, 2004 6:40 pm
Post subject: Re: PHP Classes [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Logan K. wrote:
 > What is a PHP class? I've seen a lot of talk about these and how they
 > can help you out, but I've never seen a very good explanation. I've
 > been working with PHP for just over 6 months and usually hard code
 > everything into my scripts. Recently I've begun putting frequently-used
 > things (like my MySQL connect function) in a seperate file and
 > including them. But I still haven't seen the use of classes or even
 > functions for that matter. And one more thing - how do you set up a
 > function to accept variables?
 >
 > Could someone please help me out?!?!

Whew! Thats a lot to explain in 1 post! I'll give you a nickel tour, but
it's a lot to digest and you should really look into it.

Basically, it's like grouping functions and the data those functions operate
on together in a box.

Before you can know what a class is, you have to understand functions:

$variable = "Unset";
set_function($parameter){
  global($variable);
  $variable = $parameter;
}
get_function($parameter){
  global($variable);
  echo "I am some_function, I was handed: $parameter";
  return($variable);
}

# Use some_function, giving it $parameter (a variable)
another_function("Set global variable");
get_function("Hello");
get_function("World");

Ok, here we've got a couple functions and a global variable that the two
functions work with. Great. Now imagine those two functions being applied
to different variables, lets say, $variable is someones phone number and you
want to work with different people, you're out of luck in that even.

OR imagine name problems (What if you wrote another function called
set_function(), how to keep which is which?)

Well, the different variables can be handled with array references and
parameters:

Person_construct($first,$last){
  return(array(FIRST => $first, LAST => $last));
}
Person_set_last(&$stash,$value){ $stash[FIRST] = $value; }
Person_set_first(&$stash,$value){ $stash[LAST] = $value; }
Person_get_first(&$stash){ return($stash[FIRST]); }
Person_get_last(&$stash){ return($stash[LAST]); }

Here we have a very primitive object model, since the functions
can operate on different sets of data. You can do stuff like:

$customer = Person_construct("Bob","Jones");

Person_get_first($customer); //... etc..

But, you have to remember to pass $stash to each function, and it'd be a LOT
easier to just say get_first(); However, get_first() may already be used. You
can kind of see this design pattern in non-object oriented languages and code,
stuff like the mysql_xxx accepting a resource link, or file system functions
accepting a file handle. Our "$handle" in this case is called $stash. Object
oriented classes provide this handle automatically.

Also, You might have different types of Persons, such as Employees and
Customers, they all have first and last names, but a customer doesn't earn
a salary and an employee probably shouldn't be sent catalogs on christmas.

This is where classes come into the picture. You can do stuff like:

class Person {
  public function __construct($first,$last){
   $this->first = $first;
   $this->last = $last;
  }
  public function getName() /....
  /* ...more functions, but with classes, we call them "methods" */
}
class Employee extends Person {
  public function getSalary() // Return employee salary.
}

An employee is ALSO a person, (a specific kind of person but still a person)
because it extends the Person class. So, it has a getName() method even
though none was written for it. code re-use, portions of code that
are designed to work with "Persons" can handle employees and customers
with the same ease.

The $this is like our $stash earlier, except with classes and objects,
one doesn't pass it along as the first parameter, one uses it to
call the method (invoke the function): $emplyee->getName()

There is something called a constructor and a 'new' operator, these things tell
PHP which class is associated to $employee, so it nows which bit of code
to run. Hence, no need for those long Employee_getName(&$stash) lines. (Well
that and lots of other advantages, like being able to change the class for
employee w/out breaking all your other code)

There are a LOT more things to look into, such as exceptions, interfaces,
abstraction and more.. but these are the very basics.

Jamie

--
<a rel="nofollow" style='text-decoration: none;' href="http://www.geniegate.com" target="_blank">http://www.geniegate.com</a> Custom web programming
User Management Solutions Perl / PHP / Java / UNIX
 >> Stay informed about: PHP Classes 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Can someone wxplain classes to me please - the why not wha.. - I understand what a class is but am confused as to why so many people are using them for what appear to be very simple PHP applications. I guess I dont know enough about how they are useful. for example I've just seen a "who is" application ...

Symbols used in classes. - Hi all., Probably an old quesion, but im pretty new to php, and have not had great success in searching. Ive looking over some code which contains the following terms $this -> :: And have not clue as to what they mean. I have been looking at the ph...

Best way of calling multiple classes? - I'm designing a small framework with a lot of classes (1 file per class) and want to know the best method of calling each class? Obviously I could I call each file that is used but that could be 10 or more include statements. I am wonering if there is...

Plugin system: Automatically instantiate included classes? - Hi there, I am planning to implement a plugin system, based on the observer pattern in some way, but now I am stuck with the instantiation of the plugins. I want the plugins to have their own class, abstracted from the class Plugin. So far its nothing....

json and special chars - Hi! In my DB table I've chars like עטלש but when I generate json file I show \u00e0\u00f2\u00e8\u00ec\u00f9 is it right this change in my char? Thanks
   Database Forums (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]