Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: are functions acceptable?

  1. #1
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    are functions acceptable?

    Hey guys. I was wondering is function lists still an acceptable practice even if you are using OOP? I ask because as I have been working in OOP I have found in many places were I may need a custom function but it is used in so many places it would seem more benificial just to include a list of custom functions. But I am trying to stick to todays standards so i don't want to do necisarily what is conveniant but what is accepted in the work place.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Rutek is offline Newbie
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Re: are functions acceptable?

    Maybe will you just use classes with static methods? This type of classes is often called 'Helper'.

  4. #3
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: are functions acceptable?

    I could but i was hopeing to not have to make an instance to use it. I want to use my functions much like the built in functions in php. so i want using these custome functions to be as simple as using is_set() in php now. But if it is not common practice to do it that way now days than i will turn it into a class

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: are functions acceptable?

    If you use static methods, you don't instantiate an object - you simply access the classes member functions using the scope resolution operator.
    See PHP: Static Keyword - Manual

    You might be able to use namespaces to resolve the scope to a class and use functions in the manner you wish. But they are new as of PHP 5.3 and I haven't had the time to play with them.
    See PHP: Namespaces - Manual

    However, generally speaking, if you are creating a class to act as a library, you are using OOP incorrectly.
    Last edited by John; 03-22-2010 at 10:51 PM.

  6. #5
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: are functions acceptable?

    I don't want to use a class but when i went to just make a list of functions i was told not to do that, and no matter how i go to use classes either a book or a person tells me its the wrong way and gives me contradictory views on the subject to the previous refrence i used! Is there anyone at all that agrees on how to use OO? I'm so lost, there should be a list of standards for this stuff . I'm going to go check out those two links now....

    Edit: Static will work.

  7. #6
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: are functions acceptable?

    It is because most people fail to understand what an object actually is (despite its name). An object in code should be built and used just as any tangible object. An object in code models something (and saying an object models a code library is cheating) that has attributes and functions. If you have a class with just random functions/methods, what are you actually modeling?

  8. #7
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: are functions acceptable?

    thats what i thought in the begining. If I am understanding what you are saying I think i was doing it right at first. I originally built classes that were things i could use repeatedly . such as a guest book (i use a LOT of guestbooks on my sites) which in turn i planned to make a sub class (correct word?) that extended the diaplay() method and the user() method to include a sig and turn into a forum (with a little help from another class). I also did a login class and things of that nature. I had the display method except paramaters that kept the designer from going in to my code and figured if the display was completly wrong i could always redo display() via a subclass(?) I was told that this was incorect that you never put any display code in the class. This left me with things such as making classes that did nothing more than read a directory and output a list of files. I thought the point of a class was i could just create an instance pass in the values for the constructor and it would do ALL the logic. The way people have me doing it now i have so much code that has to be done outside the object that it dosent seem anybetter than doing it all out on one page. Could you explain to me your views on creating object/classes? or if not that point me to a good article. I have to many people views in my head and i need to purge them and start new.

  9. #8
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: are functions acceptable?

    Once you understand the idea that an object represents a functional entity, then you need to learn design patterns. So instead of Googling on "how to program using OOP" search for design patterns in PHP instead. The most common "design pattern" (its actually an architectural pattern) in PHP is the Model-View-Controller. You probably already partially implement (a front controller functionally) it if you do something like:

    Code:
    switch($_GET["page"]); {
        case 
    "bar":
            require_once(
    "bar.php")
        ...

    A guest book is actually a collection of objects (this is where design patterns come in). Try creating a MySQL. First just encapsulate the basic mysql functions, but then try adding some methods of your own. For example you might have:

    Code:
    class MySQL {

        public function 
    connect($server$user$pass) {
            return 
    mysql_connect($server$user$pass);
        }

        public function 
    query($query) {
            return 
    mysql_query($query);
        }


    Now, perhaps you might have the MySQL object keep track of all its queries, or write all bad queries to a log, or add a function called insert($table, array(col1, col2, col3), array(val1, val2, val3)) and insert(...) would do a mysql_real_escape_string on val1, val2, val3 and then insert those values into the proper columns so you don't have to manually write all the queries and escape all your values manually.

    Then if you were to make a SQLite class that has the _same_exact_ functions you are on your way to the factory pattern. All you would have to do is make a DataBase object that creates either SQLite objects or MySQL objects. This is how PDO works.

    Now I would use the MVC pattern to create the GuestBook, and the GuestBook object would use a DataBase object and perhaps some other objects. Then you might consider the GuestBook object a "module" which is part of a larger system. A Login might also be a module that has a DataBase object. And the larger system has a GuestBook object and a Login object.

    When I was learning OOP, I just read all about design patterns in PHP. Once I had a solid grasp of the most popular design patterns, I implemented a completely object oriented CMS. Getting a decent architecture and design was the fun part, and that is where I learned all about OOP.

    I probably confused you.

  10. #9
    zeroradius's Avatar
    zeroradius is offline Speaks fluent binary
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    1,403
    Rep Power
    25

    Re: are functions acceptable?

    Yes confused me a bit but it at least put me on the path I need to be on. I think this is simular to what Orjan showed me. He showed me a class that handeled DB queries. Later on I tried to imulate it and it works but it seems a bit draged out. I have tried creating a function for each type of query i might want to use but I seem to have a LOT of select queries that are not suited for many tasks. I have been looking at MVC sence you mentioned it and I understand in theory what it does but do nto understand how to implement it programicly. The Model just handels the logic right? That would be the classes themselves? the View is simple enouph that is were my xhtml goes along with were i create instances of my object? is that right? I don't understand the controller, what i read says it is how users interact with the program, but in web Dev it dosent seem like somthing you need, if i am understanding it right wouldn't that be like onClick(runBlah) but that is inside the view in the program?

  11. #10
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: are functions acceptable?

    Most explanations of MVC are oriented toward programming languages such as Java where you have a real user interface. You need to find a PHP specific explanation to get a good understanding. But essentially, the controller controls the flow of the program. It is what responds to $_GET requests and then performs the needed actions. The model is what actually does
    those actions, and the view is your basic html interface. So for example, using a little OOP you might have something like:

    controller.php
    Code:
    <?php
    //I'm too lazy to implement this using objects
    switch($_GET['page']) {
        case 
    "guestbook":
            
    $model = new GuestbookModel();
            
    $comments $model->getComments();
            require_once(
    "GuestbookView.php");
            break;
        ....
    }
    GuestbookModel
    Code:
    class GuestBookModel {

            public function 
    getComments() {
                
    $comments $db->query("SELECT * FROM comments");
                
    $commentsArray $this->toArray($comments); //converts your resource into an actual array
                
    return $commentsArray;
           }

           private function 
    toArray(array $arr) {
                     
    //bleh
            
    }

    GuestbookView.php
    Code:
    <p>Hello, Here is my guest book</p>
    <?php
    foreach($comments as $comment) {
        echo 
    $comment "<br />";
    }
    Try playing with CodeIgniter or some other framework (CI is pretty simple). This is where I finally realized how the MVC work together.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  2. Would this be acceptable in the programming industry?
    By phil128 in forum General Programming
    Replies: 4
    Last Post: 05-22-2010, 04:47 PM
  3. SQL Functions - SQL Encryption Functions
    By chili5 in forum Tutorials
    Replies: 8
    Last Post: 09-04-2009, 09:40 AM
  4. SQL Functions - Math Functions
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 09-02-2009, 02:11 PM
  5. Acceptable CPU Usage
    By leon in forum PHP Development
    Replies: 2
    Last Post: 06-10-2006, 06:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

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