Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    object oriented php strategies

    Hello,

    Basically, I and another person are trying to develop a web site that deals with networking users around particular theme (yoopad.com, networking Christians around missions - what you would see there is our first attempt: now we are re-thinking and trying again.) Currently, much of what we do is procedural programming. This is my first time using php, and I've slowly been learning.

    About 2 weeks ago, I decided to look at object oriented php. It seems very useful, and I've tried it out by writing a handful of 'modules' - php classes - to preform different tasks on our website.

    Basically, I am hoping for pointers on how to do this right, or at least well.
    If anyone can recommend any good books, that would be great as well.

    On of these modules, for instance, is called onto any page and allows a user to create a new event, for instance (via a form). I know this was not recommended in the oophp tutorial I saw to but "views" together with other code, but I stored the php form and database handling and the html for the form in the same php class. The php class is initialized, and then different functions are called, for instance, there is a css_function to allow the module to write or include any css it needs, then a js_function and an html_function. There is also a php_function that is put before the header that handles the form results (the form posts to the same page it is on), and then returns those results (the database id of the new event) to the page that included and instantiated the php module. In this way, I could include this event form in many different areas and use the results in different ways.

    Here is a rough outline of what a class would look like:

    Code:
    class myclass{
    	
    	var $size;
    	//... other vars here
    	
    	function myclass($size, $event){
    		//...init code here
    	}
    
    	function php_function(){
    		//...Handle form results, if a POST is set: return results
    		if(isset($_POST['variable'])){
    			//handle form post here
    		} 
    	}
    
    	function css_function(){/*...*/}
    
    	function js_function(){/*...*/}
    
    	function html_function(){
    		?>
    <form action="myPage.php" method="post" enctype="multipart/form-data" name="testForm">
    		//... more code here...
    </form>
    		<?php
    	}
    }
    And of how it would be used:

    Code:
    <?php
    include("/folder/myclass.php");
    
    $app = new myclass("small", 28);
    
    if($result = $app ->php_function()){
    	//handle results here
    }
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    <?php $app ->css_function();?>
    
    <?php $app ->js_function();?>
    
    </head>
    
    <body>
    <h3 align="center">New Event</h3>
    
    <?php $app->html_function();?>
    
    </body>
    </html>
    It works fairly well - it is impressive seeing things just work way more than with procedural programming. And now different classes have distinct functions, and there is a clear culprit when something is going wrong.

    My code looks a little strange and maybe unconventional... that's why I am asking about it.

    Does anyone have any suggestions? Or, as I said above, and good books on the proper way to do this? By the way, our server is running php4 - how important is it we get a server with php 5?

    Thanks if anyone has any advice.

    Matthew

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You're missing an ?> before you're doctype, but I figure this is an example for our benefit.
    This looks fine. I'd recommend a PHP5 server, PHP 4 OO is sketchy at best, while PHP5 has a fully supported object core.
    The problem with modular objects in PHP4 is you're loss of core control. Since objects in php4 do not allow scope control, any added module can obtain full control of any other module at any given time. This is a nightmare if you allow user added modules (something like facebook for example).
    PHP5 also allows you to create a pattern called an object factory. This lets you create 'like' objects dynamically by extending classes and implementing interfaces in a way that makes all like objects the same functionally.
    Just because a tutorial tells you about something you shouldn't do, certainly does not mean you cannot do it. Don't take them too much to heart, unless their written by zend developers - I'd go with their recommendations then. I see no issue with creating something like a news posting object on the fly with submitted data. This lets you validate it using the class itself, which in itself has its own benefits.

    The only thing you really need to remember is that php is a static language. When you create an object, it does not stay in memory until you release it, its released at the end of any script termination. Unlike a desktop app, you can keep plugging data into the same object until you specify when it should be removed. Stay out of that trap and you'll be alright.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    mdg583 (08-07-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks a lot. I will keep giving this a try.
    I am kind of from Saskatoon as well - or at least that area. I am not there now, though.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats kewl, I'm from just north in Martensville now, but I was originally from Saskatoon. I should move though, I hate our winters >.<

    Yeah, see what you can do. As I mentioned, PHP5 OO is superior in every way compared to PHP4. You can still do a lot of it in PHP4 code, but the scope loss always frightens me. I don't believe PHP4 supports any type of extensions either, and that will increase the amount of code you require since you can't redefine and retarget you're specific needs. A quick (not really feasible) example would be a simple user:
    PHP Code:
    class User
    {
        protected 
    $userid;
        protected 
    $username;
        ...
    }
    class 
    RegisteredUser extends User
    {
        private 
    $password;
        private 
    $email;
        ...

    The first one is feasible for someone like a guest, while the second is feasible for anyone who has a password and email on top of a userid and username. Since the RegisteredUser extends user it has access to anything that is public or protected as a part of itself, and can still be compared against a user:
    PHP Code:
    $guest = new User();
    $user = new RegisteredUser();
    printf("Guest is a user? %d\n", ($guest instanceof User)); // 1
    printf("User is a User? %d\n", ($user instanceof User)); // 1 
    Its all of these little things that really make the PHP5 OO shine. Now that PHP 5+ supports custom object typehinting in parameters, it makes it really easy to make comparisons and pass correct datatypes. My wishlist for PHP oo is class scoping, nested classes and Generics.
    PHP has really evolved as an OO language. I'm quite impressed with it actually.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •