Seperation?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Seperation?

Post 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?
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

example or link please?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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-
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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();

?>
Last edited by McGruff on Wed Aug 10, 2005 11:18 am, edited 1 time in total.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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();

?>
Post Reply