Welcome Guest, Not a member yet? Register   Sign In
Determine if an object exists
#1

[eluser]Unknown[/eluser]
QUICK ACCESS: http://stikked.com/view/2a6e70a8

Save this file to 'Exists.php' in your libraries folder


I needed to write this after determining that there was no inbuilt codeigniter way to determine if any particular object existed BEFORE trying to load it.

Code:
/*
    EXISTS.PHP
    Object Existence Library
    
    A library to determine if a particular codeigniter object exists.
    This is useful if, inside your own file, you want to determine
    if a specific file exists and handle it your own way, rather
    than trying to load it and having it be handed off to the error page.
    
    Copyright (c) 2008 Jonathan Love.
    Written by Jonathan Love.
*/

For easiest access, add 'exists' to the $autoload['libraries'] array in your config/autoload.php file.
Call using $this->exists->[object]('object_name');

For example:
Code:
//Load a certain layout stylesheet if it exists, otherwise load the generic one
    if ($this->exists->view('design/stylesheets/layouts/'.$sheet))
    {
        $this->load->view('design/stylesheets/layouts/'.$sheet);
    }
    else
    {
        $this->load->view('design/stylesheets/layouts/generic');    
    }
Will attempt to load the layout stylesheet specified by $sheet, or load the generic one otherwise

Reasonably simple and elegant. Enjoy.

Got this far and still wondering where to find the code? There's a link at the top of the post




Theme © iAndrew 2016 - Forum software by © MyBB