+ Reply to Thread
Results 1 to 3 of 3

Thread: php classes help

  1. #1
    droctoganapus86's Avatar
    droctoganapus86 is offline x10Hosting Member droctoganapus86 is an unknown quantity at this point
    Join Date
    Nov 2010
    Posts
    49

    Question php classes help

    I have made a script, but it has become so big, that i want to convert it to a class. But I really don't understand how that works with variables and functions.

    Basically: How do i make a class out of this?

    I have this code :
    PHP Code:
    <div id="fb-root">
        <?php
        
    echo $fb->status;
        if (
    $fbuser):
            foreach (
    $fbhome['data'] as $mkey => $mvalue):
                    
    $href $fbhome['data'][$mkey]['from']['id'];
                    
    $name $fbhome['data'][$mkey]['from']['name'];
                    
    $message $fbhome['data'][$mkey]['message'];
                    
    $comments $fbhome['data'][$mkey]['comments'];
                    
    $appname $fbhome['data'][$mkey]['application']['name'];
                    
    $action $fbhome['data'][$mkey]['actions'][0]['link'];
                    
    $likes $fbhome['data'][$mkey]['likes']
                    
    #$date = new datemaker($fbhome['data'][$mkey]['created_time']);
                    
    ?>
                    <div class="post">
                        <a href="http://www.facebook.com/profile.php?id=<?php echo $href?>"><?php echo $name?></a>
                        <span class="message">
                            <?php echo $message;?>
                        </span>
                        <span class="links">
                            <?php if (isset($appname)):
                                echo 
    $appname;
                            
    ?>
                            ·
                            <?php endif; ?>
                            <a href="<?php echo $action?>">Like</a>&nbsp;·
                            <a href="<?php echo $action?>">Comment</a>
                        </span>
                        <ul class="comment">
                            <?php if (isset($comments['data']) || isset($likes)): ?>
                                <li class="arrow">&nbsp;</li>
                            <?php endif; ?>
                            
                            <?php if (isset ($likes)): ?>
                                <li class="links">
                                    <?php if (count($likes['data']) == 1): ?>
                                        <a href="http://www.facebook.com/profile.php?id=<?php echo $likes['data'][0]['id']; ?>"><?php echo $likes['data'][0]['name']; ?></a>
                                        <?if ($likes['count'] > 1): ?>
                                            and <?php echo $likes['count']-1?> other like this.
                                        <?php else: ?>
                                            likes this.
                                        <?php endif; ?>
                                    <?php elseif(count($likes['data']) == 2) :
                                        foreach (
    $likes['data'] as $lkey => $lvalue): ?>
                                            <a href="http://www.facebook.com/profile.php?id=<?php echo $likes['data'][$lkey]['id']; ?>"><?php echo $likes['data'][$lkey]['name']; ?></a>,
                                        <?php endforeach;
                                        if (
    $likes['count'] > 2): ?>
                                            and <?php echo $likes['count']-2;?> other
                                        <?php endif; ?>
                                        like this.
                                    <?php endif;?>
                                </li>
                            <?php endif; ?>
                            
                            <?php if (isset($comments['data'])):
                                if (
    $comments['count'] > 2):?>
                                    <li class="links"><a href="<?php echo $action ?>">View all <?php echo $comments['count']; ?> comments</a></li>
                                <?php endif;
                                foreach (
    $comments['data'] as $ckey => $cvalue):?>
                                    <li><a href="http://www.facebook.com/profile.php?id=<?php echo $comments['data'][$ckey]['from']['id']; ?>"><?php echo $comments['data'][$ckey]['from']['name']; ?></a>
                                    <span><?php echo $comments['data'][$ckey]['message']; ?></span>
                                <?php endforeach;
                            endif; 
    ?>
                        </ul>
                    </div>
                <?php endforeach;
            endif; 
    ?>
    </div>
    also, if anyone has suggestions to make this better than you're welcome

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: php classes help

    The concepts in OOP differ fundamentally from procedural programming. While any procedural program could be transformed into an OO one mechanically, the result will be low quality, according to OO design principles and best practices. Your best bet is to first study OO principles, then create a new design, then refactor existing code to match the new design. Read up on OOP features (abstraction, encapsulation, inheritance, polymorphism) and the SOLID principles.

    One issue with converting the sample is that it's not procedural. It defines no functions and uses only one (count; isset isn't a function but rather a language construct, also known as a special form). The code sample has no obvious architectural design. It wouldn't be so much a matter of conversion as construction.

    As for how variables and functions work in pure OOP, you don't have free functions and variables. Instead, every variable and function is bound to an object. Classes basically add new scopes: in addition to global and function scope, you have class and object scope. PHP isn't pure OOP, so you'll still have free functions and variables, but these should mostly be the ones PHP itself creates.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    yairsaumeth51 is offline x10Hosting Member yairsaumeth51 is an unknown quantity at this point
    Join Date
    May 2011
    Posts
    1

    Re: php classes help

    great info thenks

+ Reply to Thread

Similar Threads

  1. Help with Python Classes
    By smarto1000 in forum Programming Help
    Replies: 2
    Last Post: 08-05-2009, 10:26 AM
  2. php classes?????????
    By freecrm in forum Programming Help
    Replies: 13
    Last Post: 12-17-2008, 08:34 AM
  3. ASP.NET 2.0 Classes in Mod-mono
    By Pi606 in forum Free Hosting
    Replies: 0
    Last Post: 05-27-2008, 06:03 PM
  4. Extent of classes?
    By lambada in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 11-14-2007, 08:43 AM
  5. Different Link Classes
    By Zenax in forum Graphics & Webdesign
    Replies: 3
    Last Post: 08-04-2006, 03:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers