在OOP PHP中调用方法

I am very new at OOP, and i want to get things right at the first learning cycle.

I have a HTML form:

<?php
include("classes/Gaestebog.php");
$gaestebog = new Gaestebog();
?>
<html>
<head>
</head>
<body>
<form action="" method="post">
    <table>
        <tr>
            <td>Navn:</td>
            <td><input type="text" name="navn" value="Patrick" /></td>
        </tr>
        <tr>
            <td>Besked:</td>
            <td><input type="text" name="besked" value="Hej med dig !!" /></td>
        </tr>
        <tr>
            <td><input type="submit" name="opret" value="Opret" /></td>
        </tr>
    </table>
</form>

</body>
</html>

and a Guestbook class:

<?php
class Gaestebog {

    public function Gaestebog() {

    }

    public function getPosts() {

    }

    public function addPost() {

    }
}
?>

I want the :Guestbook to invoke the addPost method on form submission. How would i approach this?

<?php
if(isset($_POST)) {
    include("classes/Gaestebog.php");
    $gaestebog = new Gaestebog();

    $data = array(
        'title' => $_POST['title'], 
        'author' => $_POST['author'],
        'content' => $_POST['content']
        // etc. 
    ); // Do not forget to validate your data

    $gaestebog->addPost($data);
}
?>
<!-- The HTML part... --> 
get an object of your class and then call the function..

for example.. if your submit button name is 'submit' and your class name is Guestbook, then in your action page-
$guest = new Guestbook();

if($_REQUEST['submit'])      // to check that submit button is clicked
{
 $guest->addPost($data); //where data array is what to be saved from post
}

you have to include that class file in your action page like..
require '...';

You'd do this exactly as you'd expect:

if ($_POST) {
    include 'classes/Gaestebog.php';
    $gaestebog = new Gaestebog;
    $gaestebog->addPost($_POST);  // <-- example guess...
}

The basics of instantiating classes and calling their methods is not that hard. There is almost no wrong way to do it. The trick is to use object oriented programming to its full potential and structure your objects in a good way, which is something you'll need to get used to over time. See How Not To Kill Your Testability Using Statics to get a taste of what's coming your way.

In order to link the submit of the form with the addPost() you should create an action url an write a PHP script. A very first approach would be to write something like:

require_once("classes/Gaestebog.php");
$gaestebog = new Gaestebog();
$gaestebog->addPost();

the main issue with this is that you leave the responsibility of extracting the form values from the post to the Gaestebog. A better approach would be to parametrize the addPost() an let the action handle that:

require_once("classes/Gaestebog.php");
$navn = $_GET["navn"];
$besked = $_GET["besked"];
$gaestebog = new Gaestebog();
$gaestebog->addPost($navn, $besked);

in this way you decouple the view (i.e. the html page) from your model (i.e. the Gaestebog class). There are more "advanced" ways of handling this with generic action handlers and a little help of .htaccess mod_rewrite (most PHP MVC frameworks use this approach) but if you are learning I would suggest to start this way.

Finally, bear in mind that this is just the first step; one of the next things to consider is data validation and error reporting. You generally do validation on the server side and if you want to improve the user experience you also do it in the client side. The server side part is generally coordinated by the action working with the model. The client side is done in the HTML view.

HTH