Jump to content

Hall of Famer

Members
  • Posts

    314
  • Joined

  • Last visited

Posts posted by Hall of Famer

  1. Another feature from Java implemented, nice. If PHP keeps doing this they may as well consider re-creating the language based on Java for version 6, but keep their own syntax such as dynamic typing and ->/:: operators. After all, the language was originally built on C, an old-fashioned procedural language that is no longer useful nowadays with OOP as the dominant programming paradigm.

  2. Well C++'s Friend class concept is cool too, PHP may consider borrow it for next version? I know they've been adding OOP supports primarily based on Java since version 5, but Java is not the only OO programming language after all. Didnt they just get Trait? I believe this was not an idea originated from Java.

  3. Well we know that since version 5 was released, PHP has been borrowing concepts from Java and incorporating them into itself. Good examples are Static Property/Method, Abstract Class/Method, Final Class/Method, Interface, and then Namespace in PHP 5.3.x. I was wondering where Trait came from though, since apparently it is not available in Java. Is it a new concept PHP came out with on its own? Just curious.

  4. Well PHP is not a fully object-oriented programming language, which is a huge drawback. On the other hand, Ruby is quite difficult to learn, definitely not recommended for the vast number of beginner coders. I do believe there is a good reason why PHP is more popular than the other two though. I am not quite familiar with Python though, anyone mind explaining?

  5. @ Scoostah: I see, thanks for the reply and I will look into ways to do this then.

     

    @ Adam: Sorry about that, it is a mistake. Ideally such 'global' objects or registry objects should pass its reference to whatever classes/objects that use them. In this way every class has a property such as database and page. Another solution is to pass such objects in every method arguments, which is perhaps more tedious.

  6. Well it is PHP's problem that it does not implements as many OO functionalities as it should, not the developers. We all know PHP is not a fully developed language at this stage. On the other hand, I believe PHP is slowly adding more object oriented support, so it is not impossible for PHP to catch up with Java's OO features in not so distant future. I am not saying the project is quite practical at this point, but it is definitely promising with PHP's movement towards more of an object oriented programming language. For this reason I dont quite agree with the idea of dumping a project with such potential.

     

  7. Point 5 annoys me. This types of DOM work does not belong in the language itself. This promotes the idea of business being tied to UI.

     

    As for built in Ajax support. Ajax requires both server side and client side. Manipulate this idea by trying to implement it server side makes little sense. People who write web application should learn JavaScript.

     

    I see, you have a very good point. I am suggesting this not because I dont know how to use javascript though. I can code an entire javascript program without PHP just fine. I have trouble using two programming languages together though, and I wonder if this is a rare case or that many people share the same problem.

  8. Diamond PHP framework is a unique framework for PHP in a way that it is a java-like framework. It attempts to make PHP work in the Java way, the currently available version already had some nicely looking libraries. I personally think its a rather promising project. Not saying turning PHP into java is a great idea, but that it encourages programming in OO way. From its sourceforge page, it says that the project is no longer under active development. I wonder why, so do anyone of you know what happened to Diamond PHP framework and its developers?

  9. I'm not sure I would consider Ruby more difficult than Python. Then again though, once you know one language, others are easier to pickup.

     

    PHP is however the easiest of the three. Hence, so many newbs are building sites without really knowing anything about what they are actually doing.

     

    Yeah thats true, PHP makes it possible for amateurs to start off. The fact that PHP does not encourage a preferable coding convention is a problem for upstart sites going big, as the coders will eventually realize that their scripts are impossible to maintain. Would be better if PHP encourages coding in OOP, and proper design patterns.

  10. Id say you should just learn C++ without undergoing C training at all. C++ is an object-oriented programming language based on C, the syntax is similar but C++ offers much more advanced features for objects. Moreover, C++ encourages programming in the OO way which can be a problem for hardcore C programmers. If you are so used to coding an application in a pure procedural language, the switch to OOP will be more difficult than it otherwise should be. For absolute beginners procedural programming may be an easier starting point, but it is not really a problem for C++ as it also supports C's syntax. Just make sure you move into OOP right after getting used to basic stuff such as Conditionals, Loops and Functions.

  11. Oh yeah I have a few more ideas to add to the wishlist, here they are:

     

    1. Method/Function overloading:

    I believe this feature has been requested a few times, but for some reason it was never carried out. Overloading is particularly useful for constructors, if you are familiar with C++, C# or Java. I believe PHP can take advantage of it too, so we do not have to write conditional statements to check number and type of arguments over and over again. Using conditionals is fine, but testing the same or similar statement over and over again is a bad OOP practice.

     

    2. Public class with executable main() method:

    This will enable programmers to enclose everything in a class(or a public class), which in turn encourages programming in the OO way. The application I am working on has some users doing plugins, and I find these plugins quite different from one another in coding convention. Using a few of such plugins together will make the entire script messy. For this reason it is necessary to enforce programming in the OO way, and of course the more advanced way. It is also a huge leap towards fully object oriented programming if every line of code is written in class.

     

    3. More magic methods for advanced use:

    PHP's magic methods have come in handy for some programmers, and it will be nice to add more magic methods. One idea I have in mind is this __request(), which is quite similar to __call() but differ in a way that it is invoked whenever an existing method is called. It can be quite useful for situations in which you have to declare global variables or load variables from registry class in almost every class instance method. Another one is this __toInt() method, which is similar to __toString() but returns an integer value instead of a string value.

     

    4. Array as object by default:

    Although PHP has a built-in ArrayObject class, its declaration is quite tedious since you have to create an array and pass it to the constructor of ArrayObject. It would be a better idea that Array as object is the default option for PHP arrays so that we can create an Array Object with the Array() intializer or even the shortened syntax in PHP 5.4. Ideally the array's elements can be accessed using the syntax $array[index] for numeric array, and $array->key for associative array.

     

    5. DOM objects support: In PHP packages and frameworks such as Pear and CakePHP, it is possible to create a PHP table, form, link, image and other kinds of DOM objects by writing PHP codes. I've coded my own table class too, and will be doing form next. It would be better for PHP to have built-in classes for DOM objects and optimize them in a way that creating tables and forms are easier and efficient using PHP. Also applicable is built-in AJAX support, it is definitely doable as shown in projects such as xajax and phplivex.

     

    What do you think?

  12. Well I am trying to load a project I was developing into Netbeans. It looks nice, but it gives this one annoying error that comes from nowhere. In my script there is a file called inc/init.php which is included in every publicly accessible script files. This init.php defines variables such as $system, $lang, $db, $page and $registry, which is shown as below:

     

    <?php
    $dir = (defined("SUBDIR"))?"../":"";
    
    
    // Include config file, which contains constants such as DBHOST, DBNAME for later use
    $config = "{$dir}inc/config.php";
    require $config;
    
    
    // Include loader file, which defines autoloader method used to replace __autoload() function
    $loader = "{$dir}classes/class_loader.php";
    require $loader;
    $loader = new Loader;
    
    
    // Instantiating core objects
    $system = new System;
    $system->initialize();
    
    $lang = new Language();
    $lang->load();
    
    $db = new Database(DBHOST, DBNAME, DBUSER, DBPASS, PREFIX);
    $page = new Page();
    
    // Use singleton registry to store statically accessible objects, these can be used in every class file then
    $registry = new Registry();
    $registry->assign($system);
    $registry->assign($lang);
    $registry->assign($db);
    $registry->assign($page);
    ?>
    

     

    In a publicly accessible script file, it looks like this:(the below code shows the simplest "hello world" example)

    <?php
    require "inc/init.php";
    $page->settitle("Hello World!");
    $page->setcontent("This is an example of the simplest hello world");
    $system->output($page);
    
    ?>
    

     

    Nonetheless, in Netbeans I am getting errors 'this variable is not declared' whenever I use a variable(such as $system and $page) defined in an included file like init.php. It is quite annoying, do anyone of you know how I can get rid of this error? Thx.

     

  13. Well those who are used to MySQL will need to live with the fact that PDO does not have an equivalent method PDO::num_rows() to replace mysql_num_rows(). Its quite annoying, but can be easily overcome with the usage of 'SELECT COUNT' statement. As far as I know, 'SELECT COUNT' is more efficient than mysql_num_rows(), although it does take much more typing.

  14. Well if PHP is a fully object-oriented programming language, you wont need to use the new string syntax. Instead, you just do the following and it works fine:

     

    <?php
    // print hello world
    $system->output("hello world");
    // print uppercase hello world:
    $system->output("hello world"->toupper()):
    ?>
    

     

    So in the PHP version 6 I envisioned, everything is an object. The script can automatically create some basic objects such as int, float, string and array if you assign variables in traditional way, but you can also do the hard way by using the new keyword. Both options will work:

     

    Standard approach:(can be simulated in PHP 5, a bit tedious and not recommended)

    <?php
    $num = new Int(6);
    $pow = new Int(2);
    $system->output($num->power($pow);
    // print 36.
    
    $num = new Float(6.5);
    $system->output($num->floor());
    // print 6
    
    $num = new Int(6);
    $digit = new Int(3);
    $system->output($num->tofloat($digit));
    // print 6.000
    
    $lower = new Int(3);
    $upper = new Int(6)
    $randnum = $math->rand($lower, $upper);
    $system->output($randnum);
    // print a random number between 3 to 6
    
    $str = new String("text");
    $system->output($str->toupper());
    // print TEXT
    
    $array = new Array(5, 8, 4, 2);
    $system->output($array, FORMAT_ARRAY);
    // does what print_r() can do in PHP 5
    ?>
    

     

    Shortcut approach:

    <?php
    $system->output(6->power(2));
    // print 36.
    $system->output(6.5->floor());
    // print 6
    $system->output(6->tofloat(3));
    // print 6.000
    $randnum = $math->rand(3, 6);
    $system->output($randnum);
    // print a random number between 3 to 6
    $system->output("text"->toupper());
    // print TEXT
    $array = [5, 8, 4, 2];
    $system->output($array, FORMAT_ARRAY);
    // does what print_r() can do in PHP 5
    ?>
    

     

    See the point? This is what Id like to have in PHP 6, in which PHP makes sure everything is an object so that we do not have to use tedious code like the standard approach suggested, which can be such a headache. In fact, Ruby already does this kind of thing, you can use a method on a number which is not explicitly declared to be a number object but still achieves the desired operation.

     

    Another quick note is that PHP 6 may as well incorporate some unique objects such as $system, $math and $document which can be accessed everywhere in the script. These are like global objects, but they are different from traditional PHP globals in a way that you cannot overwrite or destroy them, nor can you instantiate another object of the same class. You also have to use getter and setter methods to read or append a property in the object(the math object may have no property at all). They are more similar to Singleton objects I'd say, although you do not need to have to write an additional line like $system = System::getinstance() in every class method. This is automatically done and the objects are ready to use at anytime.

     

    PHP is known for its fast implementation of advanced OOP, we already see class visibility, abstract class, interface, namespace and trait being added to PHP one by one, so we can hope for more. This is a wishlist thread anyway.

  15. Well a bug is particularly involved with what your code does. If your script has a bug, it indicates that you either makes syntax error, or that your script is insecure inherently, or perhaps the script does something different than you want it to.

     

    An issue is much more complicated. Your script may appear just fine and work normally, but it is resource-intensive because it is not optimized or that you use newbie code. This code below, for instance, is a perfect example for a script that does not have any bugs but has an issue:

     

    $i = 0;
    while($i<1){
    // load database information from mysql, sqlite or whatever
    $i++;
    
    }
    

     

    See? The script works normally on your site and you wont notice any fatal errors or warnings. It does not have a bug, but it has an issue. The while loop runs only once, even amateurs dont do this! To amend it, you can use mysql_fetch_assoc() in your while loop, or a PDO statement object if you use PDO. This will fix the issue in your script. If you remove the increment $i++ however, it will be a bug since you create an infinite loop.

     

    To summarize, a bug is a subset of an issue, an issue can be more than just a bug. Hopefully this helps.

  16. @ Josh:

    Nope, I am by no means to disrespect great coders here. A programmer aint considered amateur for not willing to use OOP, but for not able to use OOP. I understand there are programmers who have been coding in procedural style throughout their life, I wont go around calling them amateur unless they do not even understand what OOP is. In fact, PHP's support for Objects is still not yet complete. The language by itself does not encourage programming in OOP in a way like C++ and Java, which is one important reason why lots of programmers still code in procedural style since the advantage of OOP in PHP is not that evident. This is why I am making this wishlist for PHP6, since a more complete programming language by nature should encourage programmers to code in OOP.

     

     

    @Jesirose:

    Of course you have to define System class first, along with its properties and methods in PHP 5. Moreover, it has to be passed into class methods through dependency injection or declaring global keyword. This is why I said in my first post that PHP needs to enhance its support for objects by establishing some built-in magical objects that can be used in any other classes easily. This way there is no need to define a System or Math class yourself, and more importantly these classes/objects are likely to be optimized by PHP. If these objects are implicit like your last post said, the code can be further simplified.

     

    I do realize that OOP can get complicated and result in more lines than procedural programming, especially in PHP since its support for object is not yet complete. Ideally there is no need to use the new keyword on String, and instead PHP will make it a string object immediately after it sees double or single quote in a variable definition. Similarly for objects such as Int and Float, if the variable is defined as a number. I personally like Ruby's anonymous object feature, in which you can call a method on a number or string object without declaring it as variable/object in the first place:

     

    "ice is nice".length  
    

     

    in PHP, the above code can be rewritten as:

     

    "ice is nice"->length  
    

     

    This way you can use OOP in a much simpler way, which of course is not available in current PHP. This is why I am making a wishlist, for features that not yet exist(correct me if it is already possible in PHP 5.4 though).

  17. On a somewhat related note, you can see what they have planned / what people are requesting by going tohttps://wiki.php.net/rfc.  If one had an idea for something they wanted one could submit a RFC for review and it might get developed.  You'd probably need to be willing to do some/most of that development yourself though.

     

    Sounds interesting, I may as well submit my own request regarding fully object-oriented PHP. PHP6 may as well still support procedural programming since Amateurs need it, but it should strongly encourage programmers to code in OOP. C++ does a good job on this, you can write procedural code in C++ but it works much better for OOP scripts. In PHP the advantage of OOP is not that much obvious, which is why many still use lots of procedural programming.

     

    @ Ignace:

    *sigh*

     

    Newton did not know the theory of Relativity, but can you say he was a bad scientist?

     

    The fact is that in early days some theory/methodology did not exist in the world of programming. It is the ultimate intelligence of human beings to create something out of nothing, you can say the one who first proposed OOP was one of the smartest guys in the world. However, you cant say those who did not use OOP back in the days when it was not there or not yet realized as a superior programming paradigm were stupid. Hopefully this time I am being clear enough.

  18. @ Kicken:

    It would be a pity if PHP6 indeed will never come out. I am expecting much more improvement with Object oriented feature, but perhaps they have more important tasks at hands.

     

    @ KevinM1:

    I am not saying OOP = perfection. A perfect script requires more than just OOP, a programmer can code every line in OOP but still makes a crappy script. Nonetheless, fully object oriented code is one necessary step to create a perfect script. It is more reasonable to say that OOP = professional, procedural = amateur.

     

    @ Ignace:

    Nope, I do not see a need to replace single arrow -> with dot . operator. The single arrow sign is used to access object properties/methods in C++, and apparently PHP is influenced by both C++ and Java. Theres no need for PHP to become a complete copy of Java, the C syntax actually is easier to use in my point of view although Java has more OO features that can and should be implemented in PHP.

     

     

    Anyway the world of programming is moving towards OOP, it already becomes a standard for good script and in not so distant future I can see fully object-oriented script taking over procedural and half-procedural-half-OO programming. Procedural programming is for amateurs, it is good and only good for complete newbies to learn programming since starting at OO can be difficult. It is a habit that intermediate and advanced programmer should get rid of before its too late.

     

     

  19. Well it seems that the development of PHP6 has been delayed over and over again. I am not sure of the details, but seems that they are planning something big for PHP 6. Here's my wishlist for PHP6, and theres a chance some of them will be implemented in PHP 5.5.

     

    1. Fully Object-Oriented script:

    PHP is not a fully object-oriented language, which I think everyone agrees here. In programming languages such as Ruby, everything is an object. And moreover, a perfect script requires everything to be an object, thus no procedural programming at all. Unfortunately PHP is not yet a complete programming language, it has lots of non-object elements such as number, string, array and session. Although it is not impossible to create user-defined classes for string and session, their usefulness is severely limited and can be quite resource-intensive. If PHP optimizes object-oriented number, string and session, such problems will surely go away.

     

    So the idea is that, everything is an object! And moreover, for numbers, strings and arrays it is unnecessary to use the new keyword to instantiate an object(though this choice should be available). Old syntax still works, and the variable is automatically converted into objects. For instance, a string may be defined with single or double quote only(with the new keyword and parenthesis omitted), but it instantly becomes an object that can access member methods such as string::toupper() and string::crypt().

     

     

    Here is a sample of proposed code for PHP6's fully object-oriented code: String

    <?php
    //PHP5: 
    $str = "This is a String";
    $upper_str = strtoupper($str);
    echo $upper_str;
    
    //PHP6: 
    $str = "This is a String"; 
    //or:
    $str = new String("This is a String");
    $system->output($str->toupper());
    ?>
    

     

    Here is another example for Array(although ArrayObject extension kinda works already, its tedious to use...)

    <?php
    //PHP5: 
    $arr = array(New York", "Newark", "Philadelphia", "Washington", "Boston");
    $sort_arr = sort($arr);
    print_r($sort_arr);
    
    //PHP6: Both syntax should work
    $arr = ["New York", "Newark", "Philadelphia", "Washington", "Boston"]; 
    //or:
    $arr = new Array("New York", "Newark", "Philadelphia", "Washington", "Boston");
    $system->output($arr->sort());
    ?>
    

     

    So yeah, everything is an object. Every statement is carried out with either a new keyword, a single arrow -> operator or a resolution scope operator ::. I know even this does not guarantee good object-oriented design, you can easily mess up your script by writing a long lines of code that are basically no different from amateur procedural programming at all. The true greatness of OOP lies in the idea of design patterns. Nonetheless, PHP should encourage more and more programmers to take the path of OOP, no matter whether they are beginners or advanced programmers.

     

     

    2. Support for Inner/Nested Class:

    Inner/Nested classes are being well supported by programming languages such as C++ and Java(maybe Javascript too, not sure of this). The advantage is quite obvious, you can make your code more object-oriented and organized if use properly. It also provides a call-back advantage as introduced by this example in Java:

    http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html

     

     

    So you can define another class inside a class, which can access all of properties and methods of its outer class. It can be used for helper objects, which have been widely used in OOP projects. It can be used in classes that instantiate objects that belong to other closely-related classes. An inner class can be public, private or protected, while the outer class must be a public class. Heres an example of the syntax it may look like in PHP 6:

     

    <?php
    public class User{
      public $uid = 0;
      public $username;
      private $password;
      private $email;
    
      public class UserProfile{
        // codes inside
      }
    
      private class UserDataHelper{
        // codes inside
      }
    }
    ?>
    

     

    It will be great to see PHP supporting as many OOP features as possible, especially with OOP's gradually dominating the world of programming.

     

     

    3. Miscellaneous:

    Java has a system object with instance/member properties/methods and class/static properties/methods. This idea can be implemented into PHP, which effectively eliminates the traditional global variables. It also enables programmers to create fully object-oriented script, which is still impossible in PHP 5.

     

    The system object can output text, whose usage was already demonstrated in the previous examples I provided. It has properties for server, file, session, cookie and user inputs, but these can only be edited with setter methods. On the other hand, filtering and validating user input can be carried out automatically, effectively preventing newbie programmers from making mistakes such as failure to realize the script is vulnerable to SQL injection.

     

    Java allows users to create anonymous class in a similar way as anonymous function in some procedural programming languages, it is a good idea for PHP to adopt it. Anonymous class will be proved beneficial for some programmers.

     

    On the other hand, support for existing OOP features may need to be enhanced. For instance, PHP currently does not allow child class extending multiple parent classes, which can be changed in PHP 6. The support for namespace and trait can also be improved in PHP 6, which I believe is not even an issue.

     

    Again the purpose is to make it possible to create a fully object-oriented script. A fully object-oriented script may not be perfect, but a perfect script must be fully object-oriented.;)

     

     

    These are all I can think of at this moment, lemme know what you think and if theres anything else you can add to this wishlist. thanks.

  20. Well I read this thread on adobe about global variables in class methods, and someone said this:

     

    I dont use glabal vars anymore, more like a global class in the form of a Model that I inject into views (via presenters) using Swiz

     

    Thread: http://forums.adobe.com/message/4379224

     

    I was wondering how this is possible, it seems that the user has a MVC framework. I've heard of models, views, controllers and how they interact, but the concepts are still confusing to me. Can anyone of you show me an example of how a global class in the form of model can be injected into views? This can help me understand it better, and Id appreciate it very much.

     

     

  21. Well I am not an advanced programmer yet, so at this point I still havent incorporated MVC for my project. Heres the directory structure of my project, Id say its somewhere between beginners and advanced, intermediate maybe?

     

    --admincp

     

    --classes

    ----abstracts

    ----interfaces

    ----traits

     

    --css

     

    --fonts

     

    --functions

     

    --includes

    ----3rdparty

    ----lang

     

    --install 

     

    --js

     

    --images

     

    --templates

     

    And then goes every presentation-layor script files such as index.php, register.php, login.php and so on.

  22. I don't get why you have a UserProfile at all... It has no id, so it's not an entity.  It wraps data which I feel should belong to the User object, and it's only responsibilities are formatting. IMO, at best, your UserProfile IS a View. But since this formatting is part of your domain these should also be in your User class. Just like the below function would:

     

    public function getFullName() {
      return $this->getTitle() . '. ' . $this->getFirstname() . ' (' . $this->getAlias() . ') ' . $this->getLastName();
    }

     

    If you have a requirement that states that you always should address the user with it's full name. Admin and Mod are Special Case's of User so these should inherit from User (but since PHP does not support method overloading, it's better to make these responsibilities also part of User and just check with isModerator() and isAdmin() of some sort). I also fail to see how Friend and Banned could be Decorators? Banned could be a Value Object that tells how the user was banned (perm, temp) and how long (in case of temp) but not a Decorator. And Friend should be just an instance of a User as element in a _friends property array.

     

    $user1->isFriendsWith($user2);
    
    //implementation
    public function isFriendsWith(User $u) {
      Assert::true(!$this->equals($u));
      return $this->_friends->contains($u);
    }

     

    Well not really, the user profile class has methods that display certain portion of profile page content when invoked. But yeah formatting should be handled as methods for user class.

×
×
  • 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.