SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast escape164's Avatar
    Join Date
    Dec 2002
    Location
    Colorado, USA
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Good article on Drupal and OO

    I saw this today and thought it would be of benefit to share with everyone here.

    Drupal often gets criticized by newcomers who believe that object-oriented programming (OOP) is always the best way to design software architecture, and since they do not see the word "class" in the Drupal code, it must be inferior to other solutions. In fact, it is true that Drupal does not use many of the OOP features of PHP, but it is a mistake to think that the use of classes is synonymous with object-oriented design. This article will cover several of the features of Drupal from an object-oriented perspective, so programmers comfortable with that paradigm can begin to feel at home in the Drupal code base, and hopefully be able to choose the right tool for the job.
    http://drupaldocs.org/api/head/file/...opics/oop.html

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Drupal often gets criticized by newcomers who believe that object-oriented programming (OOP) is always the best way to design software architecture
    With good reason I should think. Trying to mimic OOP using class-less OOD just seems perverse and inadequate. What about TDD, for example? I suppose, in php, any efforts in the war against spaghetti are good to see but I don't think I'll be changing my programming style.

    One of the first claims they make, about lazy-loading, didn't make much sense:

    the inclusion of files defining classes must be "top-level," and not inside any function

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait... THIS is better then OOP?

    PHP Code:
    function search_excerpt($keys$text) {
      
    $keys search_keywords_split($keys);
      
    $text strip_tags(str_replace(array('<''>'), array(' <''> '), $text));
      
    array_walk($keys'_search_excerpt_replace');
      
    $workkeys $keys;

      
    // Extract a fragment per keyword for at most 4 keywords.
      // First we collect ranges of text around each keyword, starting/ending
      // at spaces.
      // If the sum of all fragments is too short, we look for second occurrences.
      
    $ranges = array();
      
    $included = array();
      
    $length 0;
      while (
    $length 256 && count($workkeys)) {
        foreach (
    $workkeys as $k => $key) {
          if (
    strlen($key) == 0) {
            unset(
    $workkeys[$k]);
            continue;
          }
          if (
    $length >= 256) {
            break;
          }
          
    // Remember occurrence of key so we can skip over it if more occurrences
          // are desired.
          
    if (!isset($included[$key])) {
            
    $included[$key] = 0;
          }
          
    // Locate a keyword (position $p), then locate a space in front (position
          // $q) and behind it (position $s)
          
    if (preg_match('/\b'$key .'\b/iu'$text$matchPREG_OFFSET_CAPTURE$included[$key])) {
            
    $p $match[0][1];
            if ((
    $q strpos($text' 'max(0$p 60))) !== false) {
              
    $end substr($text$p80);
              if ((
    $s strrpos($end' ')) !== false) {
                
    $ranges[$q] = $p $s;
                
    $length += $p $s $q;
                
    $included[$key] = $p 1;
              }
              else {
                unset(
    $workkeys[$k]);
              }
            }
            else {
              unset(
    $workkeys[$k]);
            }
          }
          else {
            unset(
    $workkeys[$k]);
          }
        }
      }

      
    // If we didn't find anything, return the beginning.
      
    if (count($ranges) == 0) {
        return 
    truncate_utf8($text256) . ' ...';
      }

      
    // Sort the text ranges by starting position.
      
    ksort($ranges);

      
    // Now we collapse overlapping text ranges into one. The sorting makes it O(n).
      
    $newranges = array();
      foreach (
    $ranges as $from2 => $to2) {
        if (!isset(
    $from1)) {
          
    $from1 $from2;
          
    $to1 $to2;
          continue;
        }
        if (
    $from2 <= $to1) {
          
    $to1 max($to1$to2);
        }
        else {
          
    $newranges[$from1] = $to1;
          
    $from1 $from2;
          
    $to1 $to2;
        }
      }
      
    $newranges[$from1] = $to1;

      
    // Fetch text
      
    $out = array();
      foreach (
    $newranges as $from => $to) {
        
    $out[] = substr($text$from$to $from);
      }
      
    $text = (isset($newranges[0]) ? '' '... '). implode(' ... '$out) .' ...';

      
    // Highlight keywords. Must be done at once to prevent conflicts ('strong' and '<strong>').
      
    $text preg_replace('/\b('implode('|'$keys) .')\b/iu''<strong>\0</strong>'$text);
      return 
    $text;

    Refactoring?
    http://drupaldocs.org/api/head/function/search_excerpt

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    Trying to mimic OOP using class-less OOD just seems perverse and inadequate.
    PHP's classes aren't exactly brilliant to start with, for example you can't add a method to a class that is already defined as talked about in the article. (You can do this is other OO languages.)

    Wait... THIS is better then OOP?
    If you think it is bad, I assume you have an idea of what you would replace it with? Can you show (or at least outline, we don't really need to see full code) how you would do this with "OO" PHP?

    Douglas
    Hello World

  5. #5
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    you can't add a method to a class that is already defined
    No problem with a decorator.

    Whatever shortcomings they perceive in php OOP, it's still much more powerful than the alternative they suggest. Why use naming conventions to create a "contract" between data and behaviour when real classes are available?

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd start by refactoring it - no, rather, I'd name everything. I'll do a mockup if I get a chance, but interpurting the multitude of one letter variables in there isnt super appealing compared to the other work I have to do. I'll come back to you a bit later (maybe tommorrow) if thats OK with you?


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
  •