SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dependencies automated (like in Java)

    Oh, I have a huge problem right now!

    So Java allows you to automatically use public classes in your current directory, or you can import all classes from another directory to use those public classes.

    I was trying to process my files (a pre-processor that deploys working PHP code) to allow me to program like in java, without worrying about dependencies, etc.

    This is much harder than I thought!

    For example, if I want class A needs to include class C, I need to open up the file "C.php". However, what if class C needs to extend class B, so it opens class B... but then class B extends from A!

    Uh... I think I need some graph algorithm or something. First off, it appears that extends/implements need to be included first off. Then, we need to include a class if it's the first time we're using new for that class. We also need to include a class if we're using a static method from it.

    Er... okay.

    I'm actually using this for my java->PHP translator, but this including problem is bigger than I thought.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     package persistent;
    import db.properties.*;
    public class PersistableObject
    {
    static StringProperty memberName=new StringProperty("name");
    static PrimaryKeyProperty id=new PrimaryKeyProperty();
    
    }
    
    Code:
     
    /*
    * Created on Sep 23, 2003
    *
    */
    package db.properties;
    public class IntegerProperty extends Property
    {
    int data;
    }
    
    Code:
     package db.properties;
    public class PrimaryKeyProperty extends IntegerProperty
    {
    
    
    }
    
    The issue arises because alphabetically, IntegerProperty comes before PrimaryKeyProperty... At this point, I think the easiest way is to add in conditional code before every extends, static, new call, and just load the file when needed (is this how the Java runtime works?).

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone know how the PHP team was going about this? I know they abandoned supporting imports, but I hope to see how far they got...

  4. #4
    SitePoint Addict shad0w's Avatar
    Join Date
    Aug 2003
    Location
    PA
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP's support of OOP is very limited compared to other languages, make sure to check out the php manual (http://www.php.net/) and read up on php classes and objects and their limitations.

  5. #5
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Brisbane, QLD
    Posts
    101
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php5 has a __call() method that you can utilize to include the file if it doesn't exist. otherwise, i'm just using a require_once() call before each "new" thing.

    every class has a require_once() for each dependency (ie, if it's extending it). but for classes being called "on the fly", essentially a factory method with the require_once() tacked on before it...

  6. #6
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php5 also has __autoload()

    PHP, as you know, is an interpreted language where your source code has to be re-examined each time a script is executed. That's great for developers, as it allows us to bypass the long wait that comes with compiled languages, and gives us plenty of opportunities to experiment and prototype quickly. The downside is that the more code you have, the longer it takes the PHP parser to work through it all on each execution—bad news for end user performance.

    Where classes are concerned, this is certainly a problem. Typically, you'll need to include all the classes that will ever be used by your application, even if the current "operation" only uses a few of them. That means the PHP parser has to do a lot of unnecessary work.

    PHP5 provides a handy new function, __autoload(), thanks to Ivan Ristic, which allows you to have a class included only when it's instantiated.

    For example, take the following class file:


    <?php
    echo ( 'Message class now being autoloaded.<br />' );
    // Autoloaded
    class Message {
    private $text = 'This is a message';
    function getText() {
    return $this->text;
    }
    }
    ?>

    Script: Message.php

    Let’s include this file in a script that uses it:


    <?php
    function __autoload($name) {
    require_once($name.'.php');
    }

    if ( isset($_GET['autoload']) ) {
    $message = new Message();
    echo ( $message->getText().'<br />' );
    }

    echo ( 'Execution finished' );
    ?>

    Script: autoload.php

    You'll notice that if you view the script normally, without adding any variables to the URL, it outputs 'Execution Finished'. But appending a "?autoload" will display "Message class now being autoloaded." as well as the output from the getText() method.

    No doubt there are other situations where __autoload() can come in handy, such as when you need to unserialize a stored object.
    Taken from the brilliant HarryF (http://www.sitepoint.com/article/1192/10)
    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems good, but it's still much too weak. I haven't tested static members yet, but it doesn't autoload the parent class!
    Perhaps this will help me somewhat, but I will still need to prepare my classes for the implemented interfaces and parent classes.

    I emailed the Japha author and he is currently working on a class loader for his project. He's basically following the java VM spec, and his idea seems to work but it could be intensive resource-wise. He loads up each class, parses them for needed classes, and then does eval("class ...definition..." ) for needed classes to load.
    If I can't get autoload to work correctly, I'll have to use that method.

    Thanks for the replies, though. I'm fairly certain autoload will come in helpful.

  8. #8
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well... here is a solution

    in each class, have a variable called '_reqd'

    Make it an array of class names that you need (assuming all your classes are in a folder). It would look something like this

    PHP Code:
    class loader
    {
        function 
    parse$which )
        {
            if( 
    is_object$which ) )
            {
                foreach( 
    $which->_reqd as $v )
                {
                    require_once 
    $v;
                }
            }
            else
            {
                die( 
    'no object provided' );
            }
        }
    }

    class 
    test_class
    {
        var 
    $_reqd = array( 'class1.php''class2.php' );
        
        function 
    test_class()
        {
            
    loader::parse$this );
        }

    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  9. #9
    SitePoint Enthusiast Morgoroth's Avatar
    Join Date
    May 2003
    Location
    Canada
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Zaire, __autoload is kinda only half implemented right now and it doesn't look like it will make it into any releases right now. The latest snapshot has a broken kinda version of it... its kinda buggy (on our nt4 test environment)

    Don't count on __autoload, heh

  10. #10
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Morgoroth
    Zaire, __autoload is kinda only half implemented right now and it doesn't look like it will make it into any releases right now. The latest snapshot has a broken kinda version of it... its kinda buggy (on our nt4 test environment)

    Don't count on __autoload, heh

    __autoload() works fine for me. parent classes, interfaces, static member variables, class constants all work fine. i'm using a PHP5 build from a couple of days ago. i don't have to use a require() or include() in any of my classes/scripts.

    php5 has a __call() method that you can utilize to include the file if it doesn't exist.
    i take it you meant __autoload()? __call() is similar in theory except it's for when you call a method that doesn't exist.
    Always open to question or ridicule

  11. #11
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eh... not bad, autoload seems to actually be pretty good.

    I d/l'ed PHP 5 a couple months ago, and my version is probably less feature-filled than sleepeasy. So, all I have to do though is call __autoload($parentClass); before any class declarations right now
    Strangely, implemented interfaces seems to autoload fine!


    Thanks for your help guys! Hopefully autoload completely working will be part of the stable PHP 5 when released.

  12. #12
    SitePoint Enthusiast Morgoroth's Avatar
    Join Date
    May 2003
    Location
    Canada
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __autoload on my dev version (form a few days ago too, heh,) is usually giving me trouble (not accepting some classes and acccepting some?)

    I dunno, it has trouble finding some classes (which are in the same directory and properly named as the ones that do work...).

    Meh, its a godsend, but on windows its proving to be a bit difficult on me...

  13. #13
    SitePoint Enthusiast
    Join Date
    Aug 2000
    Location
    Bay Area, CA; (Pasadena, CA this fall)
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP will always pass the parameter in lowercase.
    So, I just have tons of lines like this inside my autoload function:
    else if ( $class == "classa" ) include_once("../dir/ClassA.php");


  14. #14
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it is strange how for some of us __autoload() works fine when others are having trouble with it.

    Quote Originally Posted by webhappy
    PHP will always pass the parameter in lowercase.
    So, I just have tons of lines like this inside my autoload function:
    else if ( $class == "classa" ) include_once("../dir/ClassA.php");
    why don't you just lower-case all your filenames? doing what you do is ok for a small project but if you have lots of classes i can imagine it becoming a bit annoying after a while.

    incase anyone's interested this is my __autoload(). all my classes are organized in (what i think is) a PEAR style:

    PHP Code:
    function __autoload($class) {
        if (
    strpos($class'_') === FALSE) {
            
    $path $class '/';
        } else {
            
    $path str_replace('_''/'$class);
            
    $end strrchr($path'/');
            
    $path str_replace($end''$path);
            
    $path .= '/';
        }
        
    $fullPath $path $class RIOT_CLASS_EXT;
        include_once(
    $fullPath);

    Always open to question or ridicule

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you just lower-case all your filenames?
    Always a good idea this... I've been using lowercase directory/filenames for the last 18 months and I wouldn't change things for the life of me

  16. #16
    SitePoint Enthusiast Morgoroth's Avatar
    Join Date
    May 2003
    Location
    Canada
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, i think the lowercase thing might be one of the problems... stupid windows filesystem...

    but at the same time, a class named 'error' could be found, while 'errorc' could not, and both files were lowercase...

    Odd... but I withnk lowercase file names all the way through are a good idea... I always use lowercase folders, but i use camel style file naming sometimes...


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
  •