Re: [PHP] Procedural Autoloader?

2010-11-23 Thread Steve Staples
On Mon, 2010-11-22 at 15:26 -0800, David Harkness wrote:
> On Mon, Nov 22, 2010 at 3:05 PM, Richard Quadling wrote:
> 
> > Would it be overboard to use a namespace? Aren't namespaces handled by
> > the autoloader? If not autoload(), how about spl_autoloading?
> >
> 
> Autoloading is for determining the path and filename where a named item is
> defined. Namespaces only give you the path. Even with namespaces, you'd
> still need to require the files that contain the functions. Plus you'd also
> need to use the namespace everywhere you use the function because you cannot
> alias functions--only classes. :(
> 
> David

can I maybe make a suggestion?   If I have been following this
correctly, it seems as if the OP has a bunch of "non specific class"
functions, that he doesn't want in 1 big gigantic file, and include that
one huge file ALL the time...   what if the OP put some naming
conventions into the function names, and then did some kind of error
trapping on the function calls, and if the function does not exist (yet)
then call some other function to rip apart the name of the function that
was called, and then include that "file"?   I am just offering some kind
of hypothetical solution, and off the top of my head can't think exactly
how it could be accomplished.

you could always wrap all your "non class"/"custom" functions in a
function...


function checkFunction($file, $function, $args)
{
  $filename = "./function_{$file}.php";
  if(file_exists($filename))
  {
include_once($filename);
  }
  else
  {
return 'function file not exist';
  }

  if(function_exists($file .'_'. $function))
  {
return call_user_func_array($file .'_'. $function, $args);
  }
  else
  {
return "Function: {$function}() does not exist";
  }

  return 'Fire and brimstone coming down from the skies! Rivers and seas
boiling! Forty years of darkness! Earthquakes, volcanoes... The dead
rising from the grave! Human sacrifice, dogs and cats living together...
mass hysteria!!';
}

$temp = checkFunction('test', 'somefunction', array('var1', 'var2'));

then create the functions file- functions_test.php that holds all the
functions you would "group" together... 

function test_somefunction($var1, $var2, $var3 = '')
{
return $var1 .' - '. $var2;
}


This is just a thought, and yeah, it would require some rewrites, and
some forward thinking...  but could solve your issue... maybe

steve


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread David Harkness
On Mon, Nov 22, 2010 at 3:05 PM, Richard Quadling wrote:

> Would it be overboard to use a namespace? Aren't namespaces handled by
> the autoloader? If not autoload(), how about spl_autoloading?
>

Autoloading is for determining the path and filename where a named item is
defined. Namespaces only give you the path. Even with namespaces, you'd
still need to require the files that contain the functions. Plus you'd also
need to use the namespace everywhere you use the function because you cannot
alias functions--only classes. :(

David


Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Richard Quadling
On 22 November 2010 22:02, David Harkness  wrote:
> The simplest solution would be to move those functions into static methods
> of classes. Place one class in each file to organize your functions and use
> an autoloader to load the classes. You don't need to instantiate the class
> to use the autoloader--just reference it statically:
>
>    // library/Math.php
>    class Math {
>        const PI = 3.14159;
>
>        public static function sin($radians) {...}
>    }
>
>    ...
>
>    $x = $radius * Math::cos(Math::PI * 0.5);
>
> David
>

Would it be overboard to use a namespace? Aren't namespaces handled by
the autoloader? If not autoload(), how about spl_autoloading?


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Daniel Brown
On Mon, Nov 22, 2010 at 16:47, Peter Lind  wrote:
>
> Not to mention that it has nothing to do with a procedural autoloader.
> Autoloading takes place if you try to instantiate an object of a class
> that PHP doesn't know about (yet). There is no such thing for
> functions. Either refactor your code so you don't have this problem
> (The Way To Go [tm]) or make an extension. End of discussion.

Yeah, but I'm not yet done talking about it, so I may revisit it
again in a few years when everyone's forgotten all about this thread.

-- 

Network Infrastructure Manager
Documentation, Webmaster Teams
http://www.php.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread David Harkness
The simplest solution would be to move those functions into static methods
of classes. Place one class in each file to organize your functions and use
an autoloader to load the classes. You don't need to instantiate the class
to use the autoloader--just reference it statically:

// library/Math.php
class Math {
const PI = 3.14159;

public static function sin($radians) {...}
}

...

$x = $radius * Math::cos(Math::PI * 0.5);

David


Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Jason Pruim


On Nov 22, 2010, at 4:24 PM, Daniel P. Brown wrote:

On Mon, Nov 22, 2010 at 15:37, Jason Pruim  
 wrote:

Hey Everyone!

Fresh off my problem with functions and arrays I come across  
something that
I can't seem to find currently... The autoloader function that is  
in PHP 5+
works on classes... But I'm not finding anything that would do the  
same
thing on the procedural end... Such as I have a folder typically  
called
includes in my projects where I place all my function files... I  
would LOVE
to use the autoloader to be able to just load them on demand... But  
in my
quick searching/thinking I haven't found away too... So I thought I  
would
see if anyone had invented that wheel yet before I go and try and  
do it my

self :)

I may also have a misunderstanding of how it is supposed to work  
since I

don't truly understand OOP I've always done procedural...

Any help on this one would be greatly appreciated it! :)


   There's no such thing, Prune.  Autoloaders are for classes, and
the only way you could have it work for functions would be to catch
the error in the core and handle it at a lower level than your scripts
(modified core or extension), because the error generated for an
undefined function isn't a catchable fatal.  Alternatively, you
*could* write a function wrapper that utilizes function_exists() and
the like, then rewrite all of your code to use that wrapper but
how much sense does that make?  ;-P


How much sense do I ever make? :P

Maybe it's time to get a book on OOP and start learning the concept..  
Or find a better way to load my functions so I don't have to have 1  
huge functions file or tons of includes if I have them all separate...


Or maybe I can just bastardize classes enough to make something work :P

I think most of my functions border on classes anyway... Off to google  
to see if I'm right! :)




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Peter Lind
On 22 November 2010 22:40, Daniel P. Brown  wrote:
> On Mon, Nov 22, 2010 at 16:31, Nathan Nobbe  wrote:
>>
>> Shrug, if you want to really be dirty about it, you could just put a 'class'
>> atop each file of functions.
>> > class IWishTheseFunctionsWereOOInstead {} // :P
>> function firstProceeduralFunc() {
>>  // ..
>> }
>> ?>
>
>    That's not going to be economical or allow him to autoload,
> though, because the autoloader is called when the class is
> instantiated.

Not to mention that it has nothing to do with a procedural autoloader.
Autoloading takes place if you try to instantiate an object of a class
that PHP doesn't know about (yet). There is no such thing for
functions. Either refactor your code so you don't have this problem
(The Way To Go [tm]) or make an extension. End of discussion.

Regards
Peter

-- 

WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Daniel P. Brown
On Mon, Nov 22, 2010 at 16:31, Nathan Nobbe  wrote:
>
> Shrug, if you want to really be dirty about it, you could just put a 'class'
> atop each file of functions.
>  class IWishTheseFunctionsWereOOInstead {} // :P
> function firstProceeduralFunc() {
>  // ..
> }
> ?>

That's not going to be economical or allow him to autoload,
though, because the autoloader is called when the class is
instantiated.  Prune could instantiate it thusly:



 but it would be just as simple to do:





-- 

Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Nathan Nobbe
On Mon, Nov 22, 2010 at 2:24 PM, Daniel P. Brown
wrote:

> On Mon, Nov 22, 2010 at 15:37, Jason Pruim 
> wrote:
> > Hey Everyone!
> >
> > Fresh off my problem with functions and arrays I come across something
> that
> > I can't seem to find currently... The autoloader function that is in PHP
> 5+
> > works on classes... But I'm not finding anything that would do the same
> > thing on the procedural end... Such as I have a folder typically called
> > includes in my projects where I place all my function files... I would
> LOVE
> > to use the autoloader to be able to just load them on demand... But in my
> > quick searching/thinking I haven't found away too... So I thought I would
> > see if anyone had invented that wheel yet before I go and try and do it
> my
> > self :)
> >
> > I may also have a misunderstanding of how it is supposed to work since I
> > don't truly understand OOP I've always done procedural...
> >
> > Any help on this one would be greatly appreciated it! :)
>
> There's no such thing, Prune.  Autoloaders are for classes, and
> the only way you could have it work for functions would be to catch
> the error in the core and handle it at a lower level than your scripts
> (modified core or extension), because the error generated for an
> undefined function isn't a catchable fatal.  Alternatively, you
> *could* write a function wrapper that utilizes function_exists() and
> the like, then rewrite all of your code to use that wrapper but
> how much sense does that make?  ;-P


Shrug, if you want to really be dirty about it, you could just put a 'class'
atop each file of functions.



-nathan


Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Daniel P. Brown
On Mon, Nov 22, 2010 at 15:37, Jason Pruim  wrote:
> Hey Everyone!
>
> Fresh off my problem with functions and arrays I come across something that
> I can't seem to find currently... The autoloader function that is in PHP 5+
> works on classes... But I'm not finding anything that would do the same
> thing on the procedural end... Such as I have a folder typically called
> includes in my projects where I place all my function files... I would LOVE
> to use the autoloader to be able to just load them on demand... But in my
> quick searching/thinking I haven't found away too... So I thought I would
> see if anyone had invented that wheel yet before I go and try and do it my
> self :)
>
> I may also have a misunderstanding of how it is supposed to work since I
> don't truly understand OOP I've always done procedural...
>
> Any help on this one would be greatly appreciated it! :)

There's no such thing, Prune.  Autoloaders are for classes, and
the only way you could have it work for functions would be to catch
the error in the core and handle it at a lower level than your scripts
(modified core or extension), because the error generated for an
undefined function isn't a catchable fatal.  Alternatively, you
*could* write a function wrapper that utilizes function_exists() and
the like, then rewrite all of your code to use that wrapper but
how much sense does that make?  ;-P

-- 

Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread Daniel P. Brown
On Mon, Nov 22, 2010 at 15:37, Jason Pruim  wrote:
> Hey Everyone!
>
> Fresh off my problem with functions and arrays I come across something that
> I can't seem to find currently... The autoloader function that is in PHP 5+
> works on classes... But I'm not finding anything that would do the same
> thing on the procedural end... Such as I have a folder typically called
> includes in my projects where I place all my function files... I would LOVE
> to use the autoloader to be able to just load them on demand... But in my
> quick searching/thinking I haven't found away too... So I thought I would
> see if anyone had invented that wheel yet before I go and try and do it my
> self :)
>
> I may also have a misunderstanding of how it is supposed to work since I
> don't truly understand OOP I've always done procedural...
>
> Any help on this one would be greatly appreciated it! :)

There's no such thing, Prune.  Autoloaders are for classes, and
the only way you could have it work for functions would be to catch
the error in the core and handle it at a lower level than your scripts
(modified core or extension), because the error generated for an
undefined function isn't a catchable fatal.  Alternatively, you
*could* write a function wrapper that utilizes function_exists() and
the like, then rewrite all of your code to use that wrapper but
how much sense does that make?  ;-P

-- 

Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Procedural Autoloader?

2010-11-22 Thread David Harkness
On Mon, Nov 22, 2010 at 12:37 PM, Jason Pruim wrote:

> The autoloader function that is in PHP 5+ works on classes... But I'm not
> finding anything that would do the same thing on the procedural end.
>

I'll start by explaining how it typically works with classes. The Zend
Framework is a popular web/application class library. It organizes its
classes into packages much like Java, Python, and other languages, where
each package is a folder that contains other packages and classes. In Java,
packages are a language feature so you have java.util.List which is the
fully-qualified name of the List class that lives in the java.util package.

PHP doesn't have the notion of packages, though 5.3 introduced namespaces
which are similar but different. The autoloader function in PHP takes a
class name and locates the file that should define it. In Zend this is done
by separating the folder names by underscores, e.g. Zend_Http_Request. The
autoloader splits the class name on underscores and looks in registered
folders for a folder named Zend, and inside that for another folder named
Http, and inside that for a file named Request.php. Zend's autoloader class
provides more features such as aliases for folders so "ZF" would map to
"Zend".

The above is based on the convention of having one class per file. I doubt
you'll be doing that for functions, so even if PHP had an autoloading
mechanism for functions, you'd still need a way to map from function name to
file name. I suppose you could do the same as above but drop the final name
element when looking for the file. For example, the function math_trig_cos()
would map to the function cos() defined in "math/trig.php".

But it's all academic because PHP does not support such a feature. You could
probably create a PHP extension if you wanna roll up your sleeves and get
dirty in C. :)

David