The complexity of the PHP OOP approach

See below below

I'm trying to dive into OOP development in PHP, and I'm really starting to breed a headache or ulcer. I just can’t wrap my head around, the parts seem too illogical to me, and I don’t know where to start, and it really upsets me, because I consider that it is worth trying to study it, and it gives me a better overview about my code.

Yesterday I spent the whole day looking for practical examples and explanatory articles on the Internet, but now I have the feeling that it only bothers me. I need some practical advice, not examples like


class person {
     var $name;
     function set_name($new_name) {
          $this->name = $new_name;
     }
     function get_name() {
          return $this->name;
     }
}
$stefan = new person();
$jimmy = new person;

$stefan->set_name("Stefan"); $jimmy->set_name("Jimmy");

echo "Stefan full name: " . $stefan->get_name(); echo "Jimmy full name: " . $jimmy->get_name();

Examples like this make me wonder why I shouldn't (because it's all defined on the same page) just

$name = "Stefan"; 
echo "Stefan full name: ".$name;

Shorter (less code) and more obvious if you ask me?

Nou .

, , - - . 4 , , , . clients, content media, .

( , , ) -

class Database{}
class Content extends Database{}
     function showMenu(){}
     function showContent{}
class Clients extends Database{}
     function showClientList{}
     function showClientDetails{}
class Media extends Database{}

, query($qry). content :


class Content extends Database {
    public function topMenu($page){
        $db = new Database();
        $db->connect();
        $db->query("SELECT m_link, m_title, m_accesskey FROM menu WHERE m_type = 'top'"); 
        $res = $db->getResult();
        foreach($res AS $show){
            if($page == $show['m_link']){
                echo("<li id="active">$show['m_title']."</li>\n");
            }else{
                echo("<li>$show['m_title']."</li>\n");
            }
        }
    }
}

?

index.php ( ) $content = new Content(); $content->topMenu($_aGET[0]);

, , - , $_aGET[0] ( URL)

, , , , ,

$query = "
    SELECT cl.c_id, cl.c_client, cl.c_client_desc, ncc.clco_cl_id, ncc.clco_co_id, co.c_content, ncm.clme_me_id, ncm.clme_cl_id, GROUP_CONCAT(me.m_link) AS images_link
    FROM clienten AS cl
    LEFT JOIN norm_cl_co_rel AS ncc ON cl.c_id = ncc.clco_cl_id
    LEFT JOIN content AS co ON ncc.clco_co_id = co.c_id
    LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
    LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
    WHERE cl.c_client = '".mysql_real_escape_string($_aGET[1])."'
    AND cl.c_language_id = '"._LANGUAGE."' 
    AND cl.c_active = '1'
";

, , (?) ?

"" : ", "

EDIT:

, Fluffeh , . , , .

ID: CompanyName
C
Desc: C
Media:
: 
: 
: 
: 
:

( ):

ID: CompanyName
CompanyName
Desc: CompanyName is a company
Media:
: Image 1
: Image 2
: Image 3
: Image 4
: Image 5

:

class media{
    public $type;
    public $title;
    public $image;
    public $desc;
}

class client{
    public $name;
    public $desc;
}

class clientDetails{

    private $clientID;
    public $clientName;
    public $clientDesc;
    public $clientMedia = array();
    public $clientMediaFiles = 0;

    public function __construct($myID){
        $this->clientID = $myID;
    }

    public function getMyDetails(){

        $db = new Database();
        $db->connect();
        $db->query("
            SELECT c_client, c_client_desc 
            FROM clienten 
            WHERE c_client = '".mysql_real_escape_string($this->clientID)."'
        ");
        foreach($db->getResult() as $client){
            $this->name = $client['c_client'];
            $this->desc = $client['c_client_desc'];
        }

        $db = new Database();
        $db->connect();
        $db->query("
            SELECT ncm.clme_me_id, ncm.clme_cl_id, cl.c_id, me.m_id, me.m_type, me.m_title, me.m_desc, me.m_link
            FROM norm_cl_me_rel AS ncm
            LEFT JOIN clienten AS cl ON cl.c_id = ncm.clme_cl_id
            LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
            WHERE me.m_id IN(1,2,3,4,5)
        ");
        foreach($db->getResult() as $media){
            $this->clientMedia[$i]= new media();
            $this->clientMedia[$i]->type = $media['m_type'];
            $this->clientMedia[$i]->title = $media['m_title'];
            $this->clientMedia[$i]->image = $media['m_image'];
            $this->clientMedia[$i]->desc = $media['m_desc'];
            $this->clientMediaFiles++;
        }
    }

    public function displayMyResults(){
        echo "ID: $this->clientID";
        echo "&lt;div>&lt;h3>".$this->name."&lt;/h3>";
        echo "Desc: ".$this->desc."&lt;br>";
        echo "Media:&lt;br>";
        for($i=0;$i<$this->clientMediaFiles;$i++){
            echo $this->clientMedia[$i]->title." : ".$this->clientMedia[$i]->desc."&lt;br>";
        }
        echo "&lt;/div>";
    }

}
+5
4

. , , , , , .

: , , , , ...

, , , OOP.

ClientDetails

  • , clientID .
  • , .
  • , , .

, .

, , .

: , , , :

<?php

class clientContact
{
    public $name;
    public $phone;
}

class clientDetails
{
    private $clientID;      //only this class can access this variable.
    public $clientName;     // Anything can change these.
    public $clientPhone;
    public $additionalContacts= array();
    public $associates=0;

    public function __construct($myID)
    // This is a magic function that is called every time we make a new object of this class.
    {
        $this->$clientID=$myID;
    }

    public function getMyDetails()
    // This function will query the database to get it information properly
    // and populate itself with everything it needs. You could do this as part
    // of the __construct() call, but I didn't want to do TOO much code.
    {
        $sql="select clientName, clientPhone from clients where clientID=".$this->clientID.";";
        // skipping over full SQl bits...
        foreach($stmt as $clientContact)
        {
            $this->clientName=$clientContact->clientName;
            $this->clientPhone=$clientContact->clientPhone;
        }

        $sql="select clientName, clientPhone from associates where assocID=".$this->clientID.";";
        // skipping over full SQl bits...
        foreach($stmt as $clientContact)
        {
            $this->additionalContacts[$i]= new clientContact();
            $this->additionalContacts[$i]->name=$clientContact->name;
            $this->additionalContacts[$i]->phone=$clientContact->phone;
            $this->associates++;
        }

    }

    public function displayMyResults()
    // This function will simply display everything it has in a nicely format
    {
        echo "<div><h3>".$this->clientName."</h3>";
        echo "My contact phone number is :".$this->phone."<br>";
        echo "My associates are:<br>";
        for($i=0;$i<$this-associates;$i++)
        {
            echo $this->additionalContacts[$i]->name." : ".$this->additionalContacts[$i]->phone."<br>";
        }
        echo "</div>";

    }
}

?>
, ?

. .

, , :

<?php 

    $someArray=array(36, 25, 76); 
    // This could be from a query, or a form, or anything else

    for($i=0;$i<count($someArray);$i++)
    {
        $currentContact= new clientDetails($someArray[$i]);
        // This is how we create the object.

        $currentContact->getMyDetails();
        // This now tells the object to get it own data

        $currentContact->displayMyResults();
        // This now displays the data in the object to the page.
        echo "<hr>";
        unset($currentContact);
    }
?>

TINY , , , , .

, , , . ? . KPI ? , , , , , .

, HTML PHP script, , , , .

Edit:

m_image:

    $db = new Database();
    $db->connect();
    $db->query("
        SELECT cl.c_id, ncm.clme_me_id, ncm.clme_cl_id, me.m_type, me.m_title, me.m_desc, me.m_link, m_image
        FROM clienten AS cl
        LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
        LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
        WHERE me.m_id IN(1,2,3,4,5)
    ");
    foreach($db->getResult() as $media){
        $this->clientMedia[$i]= new media();
        $this->clientMedia[$i]->type = $media['m_type'];
        $this->clientMedia[$i]->title = $media['m_title'];
        $this->clientMedia[$i]->image = $media['m_image'];
        $this->clientMedia[$i]->desc = $media['m_desc'];
        $this->clientMediaFiles++;
    }

2: 100%, , . :

SELECT cl.c_id as c_id, ncm.clme_me_id as clme_me_id, ncm.clme_cl_id as clme_cl_id, me.m_type as m_type, me.m_title as m_title, me.m_desc as m_desc, me. as m_link
FROM clienten AS cl
LEFT JOIN norm_cl_me_rel AS ncm ON cl.c_id = ncm.clme_cl_id
LEFT JOIN media AS me ON me.m_id = ncm.clme_me_id
WHERE me.m_id IN(1,2,3,4,5)
+6

Java, , . , Person (P.S., ), , Person, Person.

, , - IRC Bot. User s, Channel s Server s. IRC .

, , . Channel, User Server, IRC-.

:

/**
 * Add $user to $channel.
 * @param Channel $channel
 * @param User    $user
 *
 * @throws Exception
 */
protected function addUser(Channel $channel, User $user) {
    if (!isset($this->chanList[$channel->name])) {          #Channel is not on channel list
        throw new Exception("You are not on that channel");
    }
    if (!isset($this->userList[$user->nick])) {             #User is not on global user list (New user)
        $this->userList[$user->nick] = $user;               #Add user to list
    }
    $this->chanList[$channel->name]->addUser($user);        #Add the user to the channel user list
}

, . Bot . , User Channel.

, , IRC, . , , .

0

, OOP - , , , MVC Front Controller. 4 , , . drupal, 90% .

...

$db = new Database();
        $db->connect();

db - . script db con. - .

class Database extends PDO {

  private static db = null;

  private function __construct() {
        parent::__construct(/* here get some conf data*/);    
  }

  public static getDb() {
    if(!self::db) self::db = new self();
    return self::db;
  }

  /* here you can add your custom methods like loadMenu etc. */
}

, . php, ( - , - ). extract , . - :

define('TEMPLATE_DIR', __DIR__ . '/templates');

function render($template, $variables) {
  extract($variables);
  require TEMPLATE_DIR . "/$template.tpl.php"; 
}

$variables = array('menu_links' => Database::getDb()->getMenu());

render('menu', $variables);

menu.tpl.php:

<ul>
  <?php foreach($menu_links as $link) : ?>
  <li><a href="<?= $link['href'] ?>"><?= $link['title'] ?></a></li>
  <?php endforeach; ?> 
</ul>
-1

PHP OOP MVC ( ) .

, PHP, nuklear nukes ... -, , MVCd , ( , //)

PHP MVC http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

, ( # 1) , .

, zip , .

-2

All Articles