scootstah Posted May 29, 2012 Share Posted May 29, 2012 Yup. Like @jesirose asked in the other thread, "What's the OOP version of 'Hello World' look like?" Based on the first post I guess it would be: $system->output(new String("Hello World")); Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 30, 2012 Share Posted May 30, 2012 Don't you have to define $system first, and include the class for it? Or is $system some magic global? Either way: echo 'Hello World'; seems a hell of a lot simpler Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 30, 2012 Share Posted May 30, 2012 Don't you have to define $system first, and include the class for it? Or is $system some magic global? Either way: echo 'Hello World'; seems a hell of a lot simpler System would likely be an included namespace/class, with output being a static method. So, it would actually look more like: using System; System::output(new String("Hello World")); Quote Link to comment Share on other sites More sharing options...
.josh Posted May 30, 2012 Share Posted May 30, 2012 Rule 1) $system would likely be implicit (in the same way window is implicit in javascript) and not necessary (built into the interpreter, no include or instantiation necessary) Rule 2) output can just be an alias of echo (maybe there is a nuance but nothing we care about in this scenario). Rule 3) Objects are implicit, you don't necessarily need to use the new operator to instantiate a new object. For instance, if I can pass new String("Hello World") as an argument, I can equally just pass "Hello Word" as a string object literal (same principle in js as being able to do this: console.log("THIS IS A TEST".toLowerCase()); Given these 3 "rules", echo "hello world"; would already be perfectly reasonable syntax for a fully OOP language. Just sayin'... Quote Link to comment Share on other sites More sharing options...
scootstah Posted May 30, 2012 Share Posted May 30, 2012 It's still not perfect enough. Let's jazz it up a little: System::output()->println(new String("Hello World")); Now PHP can be cool like Java! Quote Link to comment Share on other sites More sharing options...
Hall of Famer Posted May 30, 2012 Author Share Posted May 30, 2012 @ 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). Quote Link to comment Share on other sites More sharing options...
.josh Posted May 30, 2012 Share Posted May 30, 2012 HoF IMO the main reason php code is written procedural style is because it is mainly used for http protocol (making websites). OOP in a stateless environment don't really mix all that well. Obviously it can be done, but it more often than not comes out as overkill. Quote Link to comment Share on other sites More sharing options...
Philip Posted May 30, 2012 Share Posted May 30, 2012 I know what you are saying for strings and such, but I don't think it is really necessary. <?php class String { protected $_string = ''; protected $_length = 0; public function __construct($string) { $this->_string = $string; $this->_length = strlen($this->_string); } public function __get($property) { $property = '_' . $property; if(isset($this->$property)) return $this->$property; } public function __toString( ) { return $this->_string; } } echo (new String('testing'))->length; // '7' echo (new String('testing')); // 'testing' // vs... echo 'testing'; echo strlen('testing'); In any case, hopefully you've gotten it through your head that OOP is NOT a one-size fits all solution. Quote Link to comment Share on other sites More sharing options...
.josh Posted May 30, 2012 Share Posted May 30, 2012 @ 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. Really? Because that's exactly what you have been doing. You have been quite explicitly stating that OOP is "perfection" and anybody who does not use OOP is an amateur. And even still, you are still being presumptuous. You say that a programmer can be a good programmer if he uses Procedural, as long as he understands OOP. Do you not see the flaw in that statement? Either Procedural has a place or it doesn't, and that is independent of whether or not the coder knows an alternative method. So if a coder works solely in an environment where Procedural is acceptable, then there is no reason he needs to know OOP, and he is still a good coder. This is the equivalent of saying a doctor can never be a good doctor unless he knows how to do the job of all doctors, from heart surgeon to dermatologist. A doctor can specialize in skin (dermatologist) and know nothing about the heart. Does that make him a bad doctor? No! He has his knowledge and tools most relevant to his specialty. He can be a really good doctor and know nothing about hearts. Or how about an analogy that hits closer to home. You are obviously a coder. So the chances are very high you have your own circle of people who automatically think that just because you touch a computer, you are an expert on everything having to with computers. That's why your grandpa calls you up asking you to fix his shit when he's clicked on too many links from nigerian princes promising riches and 10 hour erections. And what do you say to that? "Sorry grandpa, I do web development, I don't know how to clean old-man splooge from a usb port". Does that make you a bad programmer? No! It just makes you bad at cleaning up your grandpa's taint (And I'm not judging, I too have trouble cleaning up my grandpa's taint, no worries bro). Anyways, I am by no means knocking OOP. But I love it or hate it, depending on what environment I am working in and what I am doing in that environment. As I have mentioned before, it seems that you've just gotten into it and it's really shiny and all, but you don't seem to have enough real-world experience under your belt to realize that it is not the end-all-be-all perfection that you think it is. Quote Link to comment Share on other sites More sharing options...
kicken Posted May 30, 2012 Share Posted May 30, 2012 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: Why not just use one of the other languages that supports what you like rather than try and mess with PHP? I like my PHP just the way it is. Server-side JS with NodeJS seems to be the latest fad going around and it supports syntax like you want, give it a try. For what it's worth, Functional programming is beginning to move up into the "greatest thing ever" spot to replace OOP from what I've seen. Maybe in a few more years OOP will all "amature" code and real programmers use a functional approach. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 30, 2012 Share Posted May 30, 2012 Why would a more 'complete' language automatically lead to more people using OOP? Also keep in mind that OOP is more than just using objects. OOP is more about how objects are used than anything else. It's entirely possible to write procedural style code with objects. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 30, 2012 Share Posted May 30, 2012 Or how about an analogy that hits closer to home. You are obviously a coder. So the chances are very high you have your own circle of people who automatically think that just because you touch a computer, you are an expert on everything having to with computers. That's why your grandpa calls you up asking you to fix his shit when he's clicked on too many links from nigerian princes promising riches and 10 hour erections. And what do you say to that? "Sorry grandpa, I do web development, I don't know how to clean old-man splooge from a usb port". Does that make you a bad programmer? No! It just makes you bad at cleaning up your grandpa's taint (And I'm not judging, I too have trouble cleaning up my grandpa's taint, no worries bro). PMSL, legend! Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 30, 2012 Share Posted May 30, 2012 Well you're obviously a pr0-1337 programmer, like this guy --> Write your own OO interpreted server-side programming language? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted June 5, 2012 Share Posted June 5, 2012 this good or bad coder stuff has become pretty damn annoying. You either CODE something that works properly and can handle itself under most circumstances or it doesn't. I see alot of overkill in most object oriented programing nowadays because all the big guys do it so I have to do it too... most people don't even know why they use OOP, they believe its a whole different language. Wether its echo "Hello World" or $systemlewkrlssomecrazyasssunnecessarylineofcodeWEfkefewr=ewr->println($systehm->output(new string("Hello World"))); Quote Link to comment Share on other sites More sharing options...
Hall of Famer Posted June 8, 2012 Author Share Posted June 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted June 8, 2012 Share Posted June 8, 2012 The difference between the 'tedious' approach and 'shortcut' approach has nothing to do with everything being an object. You're confusing what's known as syntactical sugar for under-the-hood functionality. It's not a matter of things being objects that magically make the shortcut work, but rather that someone programmed the interpreter to recognize certain syntactical arrangements, which are then extrapolated as being the same as the more verbose, tedious code. In compiled languages, that kind of code is sometimes literally re-written as the verbose version, then compiled. In C#, the shortcut generates the same CL* code as the tedious code, which is then compiled to bytecode. RE: Global objects - a better way to go would be to have namespaces for everything. Some things don't need to be included in every script, like math. That's how Java does it, it's how the .NET languages do it. PHP is known for its fast implementation of advanced OOP LOL, what? There's nothing advanced about what PHP has for OOP features. If anything, it's been perpetually late to the party with adding standard functionality. It's still lagging behind in some areas (see: true properties). *CL is short for Common Language. One of the features of .NET is that any language built on top of it (C#, Visual Basic, F#, etc.) is first translated into an intermediate language, and then compiled down into bytecode. That allows easy interoperability between the languages, and completely removes the necessity for a .NET programmer who only knows one language to have to learn another. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 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. Are you kidding me? OOP existed a long, long time before PHP even decided to think about implementing it. OOP existed long before PHP existed. PHP 5.3 & 5.4 have added OOP features that have existed in other languages for a long time. PHP is not known for its OOP implementation. In fact, it is rather notorious for lacking simple features that other languages have had forever. For example, PHP only just added namespaces 3 years ago in the 5.3.0 release. And, it's still crappy and not as good as other languages that use namespaces. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 8, 2012 Share Posted June 8, 2012 PHP only just added namespaces 3 years ago in the 5.3.0 release. 3 years? Has it already been that long? Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 15, 2012 Share Posted June 15, 2012 maybe more comparison operators? $haystack^=$needle (starts with) $haystack$=$needle (ends with) etc.. Quote Link to comment Share on other sites More sharing options...
.josh Posted June 15, 2012 Share Posted June 15, 2012 maybe more comparison operators? $haystack^=$needle (starts with) $haystack$=$needle (ends with) etc.. Interesting idea...not sure how I feel about that. Right now you can easily do stuff like that with for instance strpos or using regex. Not sure how useful those operators would be though. How would case-sensitivity (vs. non) be handled? That's common enough that it would have to be considered, and IMO it would be kinda lame to have two separate operators for that.. would be better as a function. On that note...perhaps cleaning up the functions in regards to stuff like case comparison.. for instance, there's no reason why there needs to be a separate strpos vs. strpos, or even strrpos and strripos .. all 4 of those could be combined to a single function. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 15, 2012 Share Posted June 15, 2012 How would case-sensitivity (vs. non) be handled? That's common enough that it would have to be considered, and IMO it would be kinda lame to have two separate operators for that There isn't case-sensitive and case-insensitive comparison operators of other types, so why would there be here? Quote Link to comment Share on other sites More sharing options...
.josh Posted June 15, 2012 Share Posted June 15, 2012 How would case-sensitivity (vs. non) be handled? That's common enough that it would have to be considered, and IMO it would be kinda lame to have two separate operators for that There isn't case-sensitive and case-insensitive comparison operators of other types, so why would there be here? Well yes, that is half my point. For that half, I agree, the point is that when it comes to current operators, case-sensitivity isn't really relevant. But for proposed operators for things like "begins with.." and "ends with.." case-sensitivity is a common issue and therefore becomes relevant. IOW sure, you can just have $= be case-sensitive but there will be enough situations and people wanting it to sometimes be insensitive that people will want a way to specify, which means a separate operator will have to be made. But since all the other operators don't consider case, having two separate operators for these proposed things would disjoint them from the spirit of other operators. So therefore my argument is that in order to accommodate case, it wouldn't make sense to have operators like this, it's better to stick with functions. In other words, operators are "lower level functions" where things like case should not be considered. There are no conditions involved. Core functions are "higher level functions" where things like that are considered (passing as arguments). There are conditions involved, that allow for variance based on circumstance. Since "begins with..." and "ends with.." will often want to consider case, they should not be expressed as operators, because situations will often demand for variance in behavior. Or another way to put it, in principle, it is essentially the same argument people make for bitching about loosely typed languages. Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 15, 2012 Share Posted June 15, 2012 How would case-sensitivity (vs. non) be handled? case insensitive (simple match) $haystack^=$needle (starts with) $haystack$=$needle (ends with) case sensitive (exact) $haystack^==$needle (starts with) $haystack$==$needle (ends with) Quote Link to comment Share on other sites More sharing options...
kicken Posted June 15, 2012 Share Posted June 15, 2012 Rather than a starts with / ends with operator I'd rather see a regex match operator, such as =~ or something. Then you could accomplish not only the starts/ends with easily but other patterns too. //starts with $str =~ /^some string/; //ends with $str =~ /some string$/; Just toss the i modifier on for case insensitive. It'd just be nice syntactical sugar though, using the appropriate functions is fine. Quote Link to comment Share on other sites More sharing options...
RobertP Posted June 16, 2012 Share Posted June 16, 2012 Rather than a starts with / ends with operator I'd rather see a regex match operator, such as =~ or something. Then you could accomplish not only the starts/ends with easily but other patterns too. //starts with $str =~ /^some string/; //ends with $str =~ /some string$/; Just toss the i modifier on for case insensitive. It'd just be nice syntactical sugar though, using the appropriate functions is fine. i like this method more Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.