Convert function to Class

Working on my OOP skills. What would be best way to convert the following function to a class?

[php]function show_action_messages()
{
if (isset($_GET[‘insert’]))
{
$action = ‘Added’;
}

if (isset($_GET[‘edit’]))
{
$action = ‘Updated’;
}

if (isset($_GET[‘deleted’]))
{
$action = ‘Deleted’;
}

if (isset($_GET[‘emailed’]))
{
$action = ‘Emailed’;
}

if (isset($action))
{
?>

Record <?= $action ?>
<?php } } [/php]

My Start
[php]class ShowActionMessages
{

}[/php]

Sounds like a great time to implement iCRUD (interface Create Read Update Delete) class?

Is the functions intent to just return a message?

Typically, classes describe an object. And the methods are actions. Just returning a string message, if that is the intent, is not a worthwhile class, and could be handled in a single method.

Good point Strider. This is not so much about the actual function itself but the use of super Global’s in a class. From my research it is not good to use them directly. I’m just not clear on exactly how you should.

But yes this function just returns a message based on a Db action.

If this is based on a DB action, it would go into a database class as a method.

As a rule, super globals should not be used in a class, as it ties implementation to a GUI. The class would just take in a parameter and use that. I regularly pass in super globals to called methods, that are expecting an Array.

If this is based on a DB action, it would go into a database class as a method.

Yeah, but that misses the point of the post.

As a rule, super globals should not be used in a class, as it ties implementation to a GUI.

Thus the post…

The class would just take in a parameter and use that. I regularly pass in super globals to called methods, that are expecting an Array.

An example would be helpful. In researching I am not seeing a clear cut solution.

You mean like

$myclass = new MyClass();
$myclass->message($_GET[‘insert’]);

Here would be a method in a class I wrote awhile ago.

[php] public function getPage( $id ) {
if ( !filter_var( $id, FILTER_VALIDATE_INT))
throw new Exception(‘Page not found!’);

    $sql = <<<SQL
    SELECT content FROM page where pageID = :id

SQL;
$stmt = $this->pdo->prepare( $sql );
$stmt->execute( array( ‘:id’ => $id ));

    if ( $stmt->rowCount() > 0 ) {
    $content = $stmt->fetchColumn();
    return $content;
    } else {
        return "Page not found!";
    }
}    [/php]

It gets called like this,

[php]echo $page->getPage($_GET[‘pageID’]);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service