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?
Seperation?
Moderator: General Moderators
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Try this...... http://uk.php.net/manual/en/ref.classobj.php
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
k, i got one thing in class.inc:
then in index.php
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-
Code: Select all
<?PHP
class getHeader {
echo "header stuff"; (like 50 lines )
}
?>Code: Select all
<?PHP
include "class.inc";
<more code>
if($pass=="rightpass") {
echo $header;
echo $addform;
}
else
{
echo $header;
echo $passform;
}
?>thanks-
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
).
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();
?>
Last edited by McGruff on Wed Aug 10, 2005 11:18 am, edited 1 time in total.
At the moment you don't because your class is wrong.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-
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();
?>