473,480 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PHP Classes

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?!?!

Jul 17 '05 #1
3 3431
In article <11**********************@f14g2000cwb.googlegroups .com>,
"Logan K." <lo*********@gmail.com> 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:

http://www.sitepoint.com/books/phpant1/

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

Jul 17 '05 #2
Michael Vilain wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
"Logan K." <lo*********@gmail.com> 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:

http://www.sitepoint.com/books/phpant1/


And also read the PHP manual

http://www.php.net/manual/en/language.oop.php
http://www.php.net/manual/en/language.oop5.php

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3
Logan K. <lo*********@gmail.com> 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

--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

Jul 17 '05 #4

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

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
1621
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
9
6621
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
12
21196
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you...
4
1795
by: john townsley | last post by:
do people prefer to design classes for the particular job or for a rangle of tasks they might encounter now and in the future. i am doing some simple win32 apps and picking classes is simple, but...
2
9475
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
2015
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
2917
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...
0
2013
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
1898
by: Amu | last post by:
i have a dll ( template class) ready which is written in VC++6. But presently i need to inherit its classes into my new C#.net project.so if there is some better solution with u then please give me...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
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...
0
7076
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...
0
6873
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...
0
4471
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...
0
2990
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
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 ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.