SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Metz, France
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A few questions...

    Hello,

    I am quite new to PHP, and I have just started building a website based on a n-tiered architecture.

    I have a few questions:
    i) Is there any way in PHP to detect calls to undefined symbols (ie object methods) ? I have set error reporting to its maximum level, and yet I get no errors when I call an undefined method. For instance, if I define a method M in class C, and then in class CC I call M() without specifying the 'C->', there are absolutely no warnings at runtime. I find this quite annoying, especially when debugging. I come from the C+/#/J2EE world, and these kinds of errors are handled by the syntactical/lexical analyzers.
    ii) Related to i), is there any way to get rid of the $ which preceds all variable names ? I mean is there any advanced PHP compiler which would allow me to use variable var instead of $var ? I know this is very subjective, but I do not think it (the $ sign) helps building readable code.
    iii) I am trying to use PEAR components, mostly DB and DataObject for the data layer. The problem I have: let us say I have defined a database table mytable with several fields named field1, field2, field3 and field4. Then I have defined a DataObject_Mytable. I use the methods setField1(), setField2() and so on. My question is: is there a way to call these methods using a mechanism similar to reflection in java ?

    For instance:
    for ( $index = 0; $index < num_fields; $index++ )
    {
    $method_name = 'setField'.$index;
    // Code to call method $method_name on related DataObject
    }

    In java, this is very easy, but i will not work with PEAR:: DataObject and the _call() function used for automatic getters and setters. I tried the call_user_func, but it does not work because (it seems) the get/set* methods are built are runtime.

    iv) What is best now in terms of design and performance ? Using all object oriented PHP with PEAR classes (DB, DataObject...), or using a J2EE server such as Jboss for the Data and business logic layers and object oriented PHP/Smarty for the presentation layer ? I know PHP has always performed very well, but there is a huge performance drop when using its object oriented features combined with a n-tiered architecture.


    Thanks in advance for your replies,
    Jerome.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In regards to i
    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    class 
    foo {}

    $f =& new foo;
    $f->bad();
    produces
    Code:
    Fatal error: Call to undefined function: bad() in /home/sweatje/public_html/test.php on line 7
    on my machine.

    ii - No, the $ is required.

    iii - constructs like
    PHP Code:
    $db->$method_name($parms); 
    should work fine for dynamic methods.

    iv - Is a can of worms Probably highly dependant on your own environment, developers skills, needs/requirements, etc.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i) not really more verbose but workable is
    if( method_exists( 'class_foo' , 'method_bar' ) ){ //etc

    ii) no [and its actually heresy to suggest such & punishment involves a hot sharp pointy stick ]

    iii) $method = 'foo' . $index ; $method() ; /*does this not work?*/

    iv) thats a question that will get you $n different answers , IMO if performance is of value to your application avoid PEAR:: DB or anything but simple abstraction , PHP's native connectivity is a feature not a bug ,.... JAVA is compiled , PHP interpreted (I know you know that) but realise the difference , porting JAVA logic to PHP does not (again IMO) work though many here will disagree strongly , PHP's native functionality and raw speed will win the day but only if you use it and do not abstract too much of it away, e.g. Smarty (originally useful) is a great example of how best to defeat the object of using PHP in the first place.

    <edit>aha beaten too it , but I got the heresy comment in </edit>

  4. #4
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Metz, France
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies.
    Regarding i), I cannot remember the code which produced the bug, but I am sure it did not work because I had forgotten "$this->" before the name of a method. It produced no errors on screen. I will try to find it.

    iii) Yes, it works, thank you.
    iv) Yes, indeed, it seems to me that PHP is losing some of its main advantages when trying to implement complex object oriented architectures. For instance, the PEAR DataObject is a good library class, but it seems to be maintained by only one person. There are no specifications, even for database abstraction layers, so that my code is very dependent on the PEAR libraries and their structures. Understand me, I am currently planning development for an ecommerce site, and I expect the site to become quite 'big'. The question I am asking myself now is: should I go and use only PHP, on all layers with PEAR, or should I use PHP only for presentation (instead of JSP) and Jboss for other layers ? Tomorrow, my site might well be serving a lot of customers and handle 100,000 or more items, so how will PHP alone perform in that case ? What I would like is to get some feedback by people who have already faced the problem, and found an "elegant" solution. I know it is quite easy to set up Jboss to work in a clustered environment, but is it possible with PHP ? How sessions are handled in a web farm ? I heard about Msession, but is it efficient and stable ?

    That's a lot of questions, I know, but I can imagine I am not the only one asking for 'enlightenments'. I am not a Java addict (I find it slow) but I like the J2EE specs and Jboss and I am not a PHP addict (I find it lacks a lot of OO features) but I like its speed (well, combined with an accelerator, and if not using too much objects).

    Jerome.

  5. #5
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Metz, France
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (To forum administrators: there seems to be a bug. I am trying to edit one of my posts, the one above in this thread, and I am always redirected to the "Delete this post" screen.)


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
  •