The Artima Developer Community
Sponsored Link

PHP Buzz Forum
Tips: File path constants, Object Oriented Programming

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Forum One

Posts: 118
Nickname: forumone
Registered: Sep, 2004

Forum One is consulting firm specializing in helping non-profits improve their online presence.
Tips: File path constants, Object Oriented Programming Posted: May 23, 2007 4:18 PM
Reply to this message Reply

This post originated from an RSS feed registered with PHP Buzz by Forum One.
Original Post: Tips: File path constants, Object Oriented Programming
Feed Title: Syntax Framework
Feed URL: http://blog.syntaxcms.org/rss.php?version=0.91
Feed Description: Finally, a place to answer Syntax questions
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Forum One
Latest Posts From Syntax Framework

Advertisement

A short missive on using the file path constants that SyntaxCMS. Relative paths can make your applicatoin more fragile than it needs to be (see dbasis), so don't do the following:

   include realpath('./../../stx/lib/documentInfo/documentInfo.php');

The correct way is (note the parens around include as well, that's our coding standard)

    include(STX_LIB_PATH . '/documentInfo/documentInfo.php);

There's a corresponding SITE_LIB_PATH for private/lib, which is for project specific includes. You can see what global path constants Syntax sets up in stx/init/init.urls.php. Also, realpath() incurs a stat call, iirc, which degrades performance.

Next:

    $doc = new documentInfo();
    $doc->set_file(realpath(SITE_PATH.'/..') . $Record->getFile());

There's a constant for the files path - PXDB_FILESTORE_PATH, so the above should be the following. The basename call is extra, because int his case Syntax is storing the filename as "/files/205_file_myfile.pdf" due to a pxdb_prefs setting.

    $doc = new documentInfo();
    $doc->set_file(PXDB_FILE_STORE . '/' . basename($Record->getFile()));

First, note above that the name $doc is a bit misleading, a better name would be $doc_info. Instead of embedding this functionality in a specific template (the detail template in this case), fetching the file information should be encapsulated as a method of the Document object by adding the following to lib/Document.class.php file. A review of the documentInfo API turns up a method that returns a document Info object for a file in a SyntaxCMS instance, named stxFactory and takes the file value as a parameter. So we add the following method to our Document class definition:

/**
  Returns a documentInfo object for the File field.
  @access public
  @return object
 /
function getFileInfo()
{
    // only need to instantiate this once per object
    if (!isset($this->_docInfo))
    {
    $this->_file_info = documentInfo::stxFactory($this->getFile());
    }
    return $this->_file_info;
}

Why is the extra effort worth it, if the starting code works? Imagine that a week from now, you're asked to display the file, and its size and type information, wherever it is listed. Are you going to copy and paste that snippet to multiple templates? What if you discover a bug in the code you cut and paste - how can you be sure you hunt down and correct all the copies? With good encapsulation your display template(s) are getting the values they need to display from a single source.

Read: Tips: File path constants, Object Oriented Programming

Topic: I'm On Twitter Previous Topic   Next Topic Topic: Posted: Syntax CMS General Module HOWTO

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use