简体   繁体   中英

OOP PHP with PHP's ArrayObject class

Question is that: Write a PHP class that inherits from PHP's ArrayObject class. Give your new class a public function called displayAsTable() that outputs all the set keys and values as an HTML table. Instantiate an instance of this class, set some keys for the object, and call the object's displayAsTable() function to display your data as an HTML table.

my answer is:

<?php

class View
{
    //definition
    private $id;
    private $name;
    private $email;

    /*
     * Constructor
     */
    public function __construct($id, $name, $email)
    {
        $this->id = $id;
        $this->name = $name;
        $this->email = $email;
    }

    /*
     * get ID
     */
    public function getId()
    {
        return $this->id;
    }

    /*
     * get Name
     */
    public function getName()
    {
        return $this->name;
    }

    /*
     * get Email
     */
    public function getEmail()
    {
        return $this->email;
    }
}


// New View List Class which extends arrayObject in PHP
class ViewList extends ArrayObject
{
    /*
     * a public function to return data
     */
    public function displayAsTable() // or you could even override the __toString if you want.
    {
        $sOutput = '<table border="1"><tbody>';
            foreach ($this AS $user)
            {
                $sOutput .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
                    $user->getId(),
                    $user->getName(),
                    $user->getEmail()
                );
            }
            $sOutput .= print '</tbody></table>';

        return $sOutput;
    }

    /*
     * return data to string
     */
    public function __toString()
    {
        return $this->displayAsTable();
    }
}

/*
 *  data(s)
 */
$data = new ViewList();
$data[] = new View(1, 'Selim Reza', 'me@selimreza.com');
$data[] = new View(2, 'Half Way', 'selimppc@gmail.com');

/*
 * final output
 */
print $data;

BUT I think I am missing something in 2D and 3D array(s) for print. Please help me out how can I print 2D and 3D in html format (in table). Thanks in Advance.

Here is the simplest solution -

<?php   

class InheritArrayObject extends ArrayObject {

    // inherits function from parent class
    public function __set($name, $val) {
        $this[$name] = $val;
    }

    public function displayAsTable() {
        $table =  '<table>';
        $table .= '<tbody>';    
        $all_data = (array) $this;
        foreach ($all_data as $key => $value) {
            $table .= '<tr>';
            $table .= '<td>' . $key . '</td>';
            $table .= '<th>' . $value . '</th>';
            $table .= '</tr>';
        }    
        $table .= '</tbody>';
        $table .=  '</table>';    
        return $table;
    } 
}

$obj = new InheritArrayObject();    
$obj->Name = 'John Doe'; 
$obj->Gender = 'Male'; 
$obj->Religion = 'Islam'; 
$obj->Prepared_For = 'ABC Org';

echo $obj->displayAsTable();    

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM