Page 1 of 1
Seperation?
Posted: Thu Nov 06, 2003 3:34 pm
by d3ad1ysp0rk
Is there anything in PHP like java classes?
because my code is getting pretty long and complicated, and a lot of things repeat, but i cant put them in a here.doc, because they include loops.. so what is the best way to seperate everything?
Posted: Thu Nov 06, 2003 3:53 pm
by maniac9
yes, PHP can do classes, and as of php5, they are handled pretty much the same as Java. PHP4 has pretty good class support as long as you use it right.
Posted: Thu Nov 06, 2003 4:01 pm
by d3ad1ysp0rk
example or link please?
Posted: Thu Nov 06, 2003 5:26 pm
by Gen-ik
Posted: Thu Nov 06, 2003 5:41 pm
by d3ad1ysp0rk
k, i got one thing in class.inc:
Code: Select all
<?PHP
class getHeader {
echo "header stuff"; (like 50 lines )
}
?>
then in index.php
Code: Select all
<?PHP
include "class.inc";
<more code>
if($pass=="rightpass") {
echo $header;
echo $addform;
}
else
{
echo $header;
echo $passform;
}
?>
now how do i make $header = all the stuff i have in my class? (a bunch of if-else, echo and mysql statement stuff)
thanks-
Posted: Thu Nov 06, 2003 7:05 pm
by McGruff
There is a widely used PEAR class library for php which might not be quite as bad as I think it is - personally I'd keep clear.
Eclipse
http://www.students.cs.uu.nl/people/voo ... /index.php on the other hand is well worth careful study.
phppatterns.com has lots of good articles on OOP design.
First I'd rename your class. getSomething (and setSomething) are standard ways to name interface accessor methods and it could be confusing to name your class like this. Classes are "nouns" or things and methods are "verbs" or actions, so "Header" might be a good name.
Can you make a setPageVars() method, and a printHeader() method? The first would declare all the vars you need, the second would print them in an html template.
Try to create small-sized methods to carry out specific tasks - such as getting a db query result. The setPageVars() method would be a controller making calls to these single-function, "private" methods (yes there are private methods in php - provided you don't call them externally

).
Code: Select all
<?php
$header = new Header;
$header->setPageVars($args);
$header->printHeader();
?>
Posted: Thu Nov 06, 2003 7:27 pm
by Gen-ik
LiLpunkSkateR wrote:k, i got one thing in class.inc:
now how do i make $header = all the stuff i have in my class? (a bunch of if-else, echo and mysql statement stuff)
thanks-
At the moment you don't because your class is wrong.
Here's a small example of how to use a PHP class.... I would suggest reading more tutorials and info about them though.
Code: Select all
<?php
class testClass
{
function echoDate()
{
echo date("jS F Y");
}
function echoDate2()
{
$this->echoDate();
}
}
$myClass = new testClass();
$myClass->echoDate();
echo "<br>";
$myClass->echoDate2();
?>