Jump to content

Hall of Famer

Members
  • Posts

    314
  • Joined

  • Last visited

Posts posted by Hall of Famer

  1. :facepalm:

     

    Do we really need to have this discussion again?

     

    Nope, not really. I was just posting my advices/feedback to the OP, it was not meant for a thorough discussion. It is his thread anyway, and wont help him much if we discuss something unrelated to the main topic.

  2. Well what I can say...

     

    First of all, congratulations on what you have accomplished so far, I suppose the code is working properly? I like how you pay attention to security and not to trust user input data, thats a very important step.

     

    Second, try separating your business logic from presentation layer. A true professional script requires this separation of concerns, you can have a controller file handling business logic/commands, and a view file displaying HTML results.

     

    On the other hand, use OOP. Procedural programming is for amateurs and only good for newbie's learning process, use objects and object oriented design can improve your code dramatically, especially for future extensibility/reusability.

  3. A PHP collections framework to manipulate collection of objects, similar to Java's. Lets face it, PHP's array is not that useful despite being much more powerful than Java's, and more importantly it is not object and thus the flaws are pretty much impossible to fix. Start with SplFixedArray and work on a collections framework from it, I believe its doable. In fact I am creating a PHP collections framework myself, working on HashMap right now. However, considering User-created classes will never be as efficient as PHP's built-in classes, it is definitely better to have a built-in collections framework in PHP's core.

  4. Well good OOP design requires no global variable or at least minimum usage of globals. Technically you can achieve the same functionality as global variables with Singleton/Registry pattern, but these are often considered anti-patterns.

  5. umm I thought there is a section dedicated to frameworks and CMS discussion? Anyway, it seems that Joomla and Drupal are the kings. The latter draws my attension since Symfony claims that Drupal uses its framework, which should be very good for programmers. I downloaded Drupal's files but did not find anything like that. Perhaps they find a way to hide their source code from public? Wonder how thats even possible lol.

  6. Best guess without looking at how the class is implemented: Future instances of SplFixedArray (or subclasses there of) re-use already allocated memory.

     

    Allocating a chunk of memory takes time as the system has to search for a block big enough to fulfill the request and also update allocation tables. There may also be some paging/swapping involved that slows things down. Since you are re-using the same variable name when you assign new instances of SplFixedArray and it's subclasses the previous instance can be destructed and it's memory released. The way PHP works though is it does not actually "release" the memory, it just goes into a pool that PHP itself will manage so that future allocations for variables won't (generally) have to allocate memory from the system, resulting in faster performance.

     

    So in your first loop, each time you make a bigger and bigger SplFixedArray class PHP will have to allocate from the system enough memory to handle that request. In your second loop though, all that memory is still allocated (just unused), allowing the new class instances to just quickly grab a chunk of memory and skip the allocation process.

     

    Interesting, thanks for your comment. I guess performance test indeed can get a bit tricky if done in an inappropriate way, I will create two different script files and run the tests separately in future then, for any types of performance tests I mean.

  7. Well I'm thinking about replacing most of my arrays with known size by PHP's SplFixedArray since it's faster and more efficient. I tested it on my site, the result is not surprising, SplFixedArray is indeed faster in most occasions. What astonished me was that after I created this subclass called NumericArray by extending PHP's SplFixedArray, the child class turns out to be even faster. Here's the code I used to generate the result, which is similar to what the sample code from PHP.net:

     

    <?php
    
    class NumericArray extends SplFixedArray{
    
    }
    
    for($size = 100; $size <= 102400; $size *= 2) {
        echo "<br>";
        echo PHP_EOL . "Testing size: $size" . PHP_EOL;
        echo "<br>";
    
        for($s = microtime(true), $container = Array(), $i = 0; $i < $size; $i++) $container[$i] = NULL;
        echo "Array(): " . (microtime(true) - $s) . PHP_EOL;
        echo "<br>";
    
        for($s = microtime(true), $container = new SplFixedArray($size), $i = 0; $i < $size; $i++) $container[$i] = NULL;
        echo "SplArray(): " . (microtime(true) - $s) . PHP_EOL;
        echo "<br>";
    
        for($s = microtime(true), $container = new NumericArray($size), $i = 0; $i < $size; $i++) $container[$i] = NULL;
        echo "NumericArray(): " . (microtime(true) - $s) . PHP_EOL;
        echo "<br>";
    }
    
    ?>
    
    

    And here are the results for the smallest, median and largest array size:

    Testing size: 100
    Array(): 3.09944152832E-5
    SplArray(): 2.28881835938E-5

    NumericArray(): 1.19209289551E-5

     

    Testing size: 3200
    Array(): 0.000507116317749
    SplArray(): 0.000546216964722
    NumericArray(): 0.000308036804199

     

    Testing size: 102400
    Array(): 0.0228500366211
    SplArray(): 0.0149321556091
    NumericArray(): 0.0126581192017

     


    Perhaps the results will be different if I run at even larger size such as 1000000, but my server apparently runs out of memory at that stage so I cannot justify this statement. Anyway, the result does not seem to make sense. To my understanding the subclass should always be slower than the parent class. The result is not much different even if I add a constructor method for the child class, although this will somewhat reduce the gap between the performance of two classes.

     

    Maybe the test code I ran had flaws in it? I am not quite sure since thats what people on PHP.net runs the test code. I did carry out multiple test runs, there were slight variations but in all test runs the NumericArray was always faster. The only possible explanation is that the child class loses part of the functionality from this parent class, but I dunno if this is true.

     

    Sure there is a good chance that the operation on array elements is slower for the child class, but it is still difficult to interpret why the child class is faster to construct. Can anyone explain this phenomenon that I find absurd?

  8. I think it depends on whether you want several additional fields to be stored for admin users, which are completely irrelevant for regular members. If this is true, you may subclass the user/member class with additional properties being neatly mapped to database fields. If it is just a status however, you are better off simply defining an additional property in your user/member class to label the specific user as an admin user. It totally depends on the application you have.

     

    And you may want to consider Data Mapper enterprise pattern to do the trick of fetching a user/admin object with a supplied criteria. These are good examples, I believe its quite flexible this way. You may extend its functionality to handle multiple criteria, say both a username and a password. The mapper can be extended to handle more than just single selection statement, you can read Matt Zandstra's book for more reference.

     

    $user = $userMapper->findByID(1);
    $user2 = $userMapper->findByUsername("Admin");
    $user3 = $userMapper->findByEmail("youremail@gmail.com");
    $user4 = $userMapper->findByIP("127.0.0.1");
    
    
    
  9.  

    You can use PDO, it is a really powerful class and very object oriented. All you have to do is this:

     

    $pdo = new PDO($dsn, $user, $password);
    $stmt = $pdo->prepare('SELECT * FROM tablename WHERE column1 = ? AND column2 = ?');
    $stmt->execute(array($field1, $field2));
    $obj = $stmt->fetchObject();
    

     

    Can a mod delete the code in my last post? I did not think much when I quoted it from php.net and posted it with some minor editions, but its not a very good OOP practice. Can be misleading for newbies, so its better to let it go. Thx.

  10. My point was, as I wrote, to show that one can use objects in a purely procedural code. That was simply the first and best example I had available.

     

    Alright then, guess I should apologize for posting a bad example of non-OO PDO usage. I pretty much copied it from PHP.net since I realized my actual database class and domain model objects will be too long for a newbie to read. The reference is here:

    http://www.php.net/manual/en/pdo.prepare.php

     

    But yeah, you are right these are bad examples with poor OOP practices. I will edit out my post so as not to confuse newbies with amateurish procedural coding, thanks for pointing it out.

  11. You can use objects in strictly procedural code, like in this example.

     

    umm whats your point? That one was just an example from a typical PHP site, I have my own database, query object, domain model and mapper classes to handle OO script and I dont feel it necessary to post it here as it will require too many lines of code and files. Id personally never use that style for an OOP application lol.

  12. You can use PDO, it is a really powerful class and very object oriented. All you have to do is this:

     

    $pdo = new PDO($dsn, $user, $password);
    $stmt = $pdo->prepare('SELECT * FROM tablename WHERE column1 = ? AND column2 = ?');
    $stmt->execute(array($field1, $field2));
    $obj = $stmt->fetchObject();
    
  13. Congratulations on finding a desired framework for yourself. For beginners really, Symfony and Zend aint the appropriate choices for them due to the learning curve issue. CakePHP, Codeigniter and Yii are much easier to get used to, well Symfony and Zend are for advanced and professional programmers.

  14. Id strongly recommend you to overhaul your script before applying them into frameworks, since its difficult to convert a spaghetti code into a neatly designed structure of script in one step. But anyway if you want to do it fast, Codeigniter seems to be the framework that integrates with non MVC script the best. 

  15. I wouldnt say no one still uses PHP 4, but its definitely no need for any frameworks to compensate for PHP 4 users. They are minority nowadays, and PHP4 is not powerful enough to code most web applications. They are mostly for old applications written in the early days, no new softwares should be written with PHP 4 except in extremely rare cases when its the only available choice.

  16. - is it better to code OO?

    Yes, OOP is the standards for good programming practices, it is a step necessary for amateur programmers to improve into professionals. If you are doing programming for a living, chances are you wont find a job unless you are comfortable about OOP. OOP offers many things other paradigms either cannot or can only mimic poorly, good examples are Polymorphism, Encapsulation, Modularization/Reusability, Better Maintenance and Easier teamwork. In a perfect script everything is an object, you cant be perfect, but you can approach it as much as possible.

     

    - are frameworks usefull? necessary?

    They definitely are, but whether its necessary or not to use Frameworks will depend on your project. Sometimes Id say go for it, sometimes you can survive as well without them.

     

    - all big applications, should they be OO?

    Yes, successful big applications all use OO, theres a reason for that. Large projects typically rely considerably on teamwork, which is difficult if your application uses procedural style. The benefits of OOP  I mentioned earlier also are much more evident in larger projects than smaller ones. You can build a fansite with Procedural programming just fine, but for a professional website it wont work.

     

    - what's the speed at what you guys are learning? I'm reading about PHP for 1 year now, a good time to start OO?

    Id say its a good time to consider OOP, but only after you are comfortable with the basics of PHP. 1 year is not a short learning time, I'd assume you are already quite good at procedural PHP so its about time to improve yourself further. Remember, you cant be an advanced or professional programmer until you can code in OOP.

  17. So Godaddy's default PHP version is 5.3? Thats nice, many shared and free hosts still run PHP 5.2 as default, though some do allow you to upgrade with one click. My software requires PHP 5.3, so I just have to tell users getting errors to upgrade their PHP. Its kinda bad when you have to repeat the same line over and over again lol, but this cant be helped until webhosts aint stubborn enough to stick to an old fashioned php version.

  18. Symfony and Zend Framework are always the top frameworks to use, they just aint that easy for beginners but this should not be an issue for you if you are an advanced programmer. If you just want to learn more about how other frameworks design their own MVC framework so that someday you will make your own, Id suggest codeigniter.

  19. Id say its better to use reflection than magic methods, but anyway its never a good practice to access private fields in another class. You should do it only when you have a very good reason,

     

    And I dont quite understand the logic of User class extending Auth. I'd normally assume that Auth is a sub-category of User, as a guest, bot and banned user are all technically users but auth is for admins, mods and other staff members? Its not a big deal in early design stage, but you need to have a clear logic. OOP is more than just using object oriented code, it requires object oriented way of thinking and design. But for now I wouldnt say you are doing a bad job, people just have to start from somewhere.

  20. No worries, glad it works. Anyway you need to know that you dont want to compare string values, unless you have a really good reason to do this. You usually can compare numbers, but make sure to check whether the variables do hold numeric values so that you aint comparing numbers to string or string to string. In the example above, I was comparing two objects. Its a common practices for programming languages such as Java and C#, but for PHP it can get tricky too. The good thing is that it works for DateTime objects, as well as most PHP builtin extensions.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.