|
revmoo posted:I've never seen a use case of switch() where I thought it made sense. At best you're abbreviating what should be a function or class, depending on complexity, and at worst you've written a bunch of lovely if statements that could have been done a smarter way. The last switch statement I wrote was given a database info object, open an appropriate table factory for it. I could have written code:
code:
|
# ? Mar 7, 2015 07:36 |
|
|
# ? Jun 4, 2024 06:23 |
|
If you litter your code with switch statements that check which database you're interacting with, adding another supported database is suddenly difficult because now you need to find and change every switch statement, if you're trying to track down a bug specific to one database implementation you need to track down every point at which the implementations diverge, etc.. A better way of implementing it is to have a dbInfo subclass for every database you're interacting with, that encapsulates all your database specific logic - the basic interface might have a method called getTableFactory that returns something that implements the TableFactory interface, where OracleDbInfo returns an OracleTableFactory from that method, PostgresDbInfo returns a PostgresTableFactory, etc.. Then when you need to add MySql support, all you need to do is write a new dbInfo subclass that implements the appropriate functionality. This is why polymorphism is generally preferred over switch statements. That's not to say you should never use switch statements - writing a ladder of if statements because "switch statements are bad " is cargo-cult shitfuckery at its finest. But if you find that a switch statement looks like a good tool for the job, perhaps you should consider using polymorphism instead.
|
# ? Mar 7, 2015 09:13 |
|
Jabor posted:If you litter your code with switch statements that check which database you're interacting with, adding another supported database is suddenly difficult because now you need to find and change every switch statement, if you're trying to track down a bug specific to one database implementation you need to track down every point at which the implementations diverge, etc.. A better way of implementing it is to have a dbInfo subclass for every database you're interacting with, that encapsulates all your database specific logic - the basic interface might have a method called getTableFactory that returns something that implements the TableFactory interface, where OracleDbInfo returns an OracleTableFactory from that method, PostgresDbInfo returns a PostgresTableFactory, etc.. Then when you need to add MySql support, all you need to do is write a new dbInfo subclass that implements the appropriate functionality. This is why polymorphism is generally preferred over switch statements. How about : code:
|
# ? Mar 7, 2015 16:39 |
|
Oh, we're doing JS in the php thread now? code:
|
# ? Mar 7, 2015 17:18 |
|
revmoo posted:Oh, we're doing JS in the php thread now? It was actually Java, but you could apply the same ideas to PHP with constants: code:
|
# ? Mar 7, 2015 18:11 |
|
Master_Odin posted:I personally hate the way you've written your if statements and think that the switch is cleaner to read overall. This is the crux of the issue, you're making an aesthetic argument and that's going to boil down to personal preference. Personally I like being able to see 150+ conditionals on a single page of my coding monitor, and switch statements eat up line counts like crazy. I'd love to see a technical argument in favor of switch statements, though. They seem really popular among C developers, but that's probably a result of the lack of OOP. vvv Good one! revmoo fucked around with this message at 20:04 on Mar 7, 2015 |
# ? Mar 7, 2015 18:23 |
|
revmoo posted:This is the crux of the issue, you're making an aesthetic argument and that's going to boil down to personal preference. Personally I like being able to see 150+ conditionals on a single page of my coding monitor, and switch statements eat up line counts like crazy. http://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c
|
# ? Mar 7, 2015 19:12 |
|
Master_Odin posted:How about : The overall architecture here is straight-up wonky, it's hard to offer suggestions beyond "rewrite the whole thing to fix some of the worst bits". Where does toType come from? What is the point of the field class anyway?
|
# ? Mar 8, 2015 00:48 |
|
What have I done? Personally I was arguing more that switch statements aren't a good choice for high-level flow control. I'm really only inclined to use them where there's one value comparison occurring, and I'm pretty certain there's nothing else that's going to factor in. I hate seeing this: php:<? public function myEditAction(Request $request) { switch($request->get('mode') { case 'edit': // Tons of poo poo break; case 'publish': // Tons of other poo poo break; } } ?> This is fine: php:<? public function annoyMyThing($thing, $annoyType) { switch($annoyType) { case self::ANNOY_TYPE_GENTLE: case self::ANNOY_TYPE_MEDIUM: // deprecated $thing->poke(); break; case self::ANNOY_TYPE_VIOLENT: default: $thing->slap(); break; } } ?> php:<? switch(true) { case ($myThing->getThing() == $anotherBit->getBit()): case ($myThing->getThing() == $something->getThing()): $myThing->derp(); break; case: ($something->getThing() != $what): default: $myThing->flap(); break; } ?> php:<? switch(true) { case ($x instanceof MyClass): case ($x instanceof AnotherClass): $x->doSomething(); break; case ($x instanceof CrapClass): $x->doCrap(); break; default: throw new RuntimeException('eh?); } ?> What's the likelihood of needing to refactoring it? That's entirely down to where in your code it lives, and what it's doing.
|
# ? Mar 8, 2015 01:08 |
|
What's a recommended resource for learning about Object Oriented PHP? I'm having a bit of trouble wrapping my head around it compared to procedural style. Lots of results on google, but specific suggestions from people who have used them would be appreciated
|
# ? Mar 8, 2015 15:46 |
|
Experto Crede posted:What's a recommended resource for learning about Object Oriented PHP? I'm having a bit of trouble wrapping my head around it compared to procedural style. This is a decent overview that includes a short comparison between procedural and OO styles: http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762 Are there specific things you're struggling with?
|
# ? Mar 8, 2015 16:19 |
|
Anyone here got experience with Laravel? Opinions? Pretty new to PHP as a whole.
|
# ? Mar 9, 2015 17:48 |
|
Dougie McdDouger posted:Anyone here got experience with Laravel? Opinions? It's pretty rad!
|
# ? Mar 9, 2015 18:01 |
|
It's the best framework for PHP at the moment (and probably for a decent while into the future)
|
# ? Mar 9, 2015 18:15 |
|
RE: Laravel Version 5 is pretty new, the docs are good and it has lots of great features. It seems like most package providers are updating support for 5.x but most guides you find online for doing things and most packages are for 4.2. I personally am not using 5.x yet because some dependencies are not fully supported on 5.x and the upgrade process is a PITA! I totally recommend signing up for an account at http://laracasts.com. It's worth every penny if you're just getting started and don't want to have to learn the hard way.
|
# ? Mar 9, 2015 18:34 |
|
Also Laravel has great documentation and resources (e.g. Laracasts)
|
# ? Mar 9, 2015 18:35 |
|
Laracast seems interesting. Do you need much prior knowledge of PHP prior to their tutorials? I'm perfectly happy with HTML, CSS. I've only done the code academy course on PHP.
|
# ? Mar 9, 2015 18:45 |
|
Dougie McdDouger posted:Laracast seems interesting. Do you need much prior knowledge of PHP prior to their tutorials? I'm perfectly happy with HTML, CSS. I've only done the code academy course on PHP. Tbh you could just jump right in as long as you have a solid theoretical understanding of MVC and ORMs/database abstraction layers and writing queries. If not, you might want to bite off some smaller pieces first.
|
# ? Mar 9, 2015 19:49 |
|
Dougie McdDouger posted:Laracast seems interesting. Do you need much prior knowledge of PHP prior to their tutorials? I'm perfectly happy with HTML, CSS. I've only done the code academy course on PHP. There are some php beginner tutorials as well as some intro to OOP and MVC stuff too.
|
# ? Mar 9, 2015 20:01 |
|
revmoo posted:Tbh you could just jump right in as long as you have a solid theoretical understanding of MVC and ORMs/database abstraction layers and writing queries. If not, you might want to bite off some smaller pieces first. Any suggestions? That is jargon to me currently.
|
# ? Mar 9, 2015 20:26 |
|
Read the book 'PHP Objects, Patterns and Practice' It won't get you all the way there but it will start you down the road. It's a great book and it dispenses with the hello world crap and jumps right in with the expectation that you know what a for loop is but you don't know what a front-controller pattern is.
|
# ? Mar 9, 2015 20:30 |
|
revmoo posted:Read the book 'PHP Objects, Patterns and Practice' http://www.amazon.com/dp/1430260319 specifically.
|
# ? Mar 9, 2015 20:39 |
|
Cool. I'm getting there slowly. Essentially, what I'm trying to get to is something like this: https://goonauth.goonrathi.com/login but for a different website and forum (based on Xenforo). I've also been going through some free Laracasts i've found: the fundamental lessons.
|
# ? Mar 10, 2015 18:01 |
|
Dougie McdDouger posted:Cool. I'm getting there slowly. Seems it's on Github, you can easily clone this to your dev environment, run code:
code:
Don't have a dev environment? http://laravel.com/docs/5.0/homestead Install vagrant and oracle virtual box!
|
# ? Mar 10, 2015 18:22 |
|
is that the hilariously overengineered goonauth that requires LDAP/AD and assigns users to OUs at auth time? iirc it's laravel 4
|
# ? Mar 10, 2015 18:51 |
|
DarkLotus posted:Seems it's on Github, you can easily clone this to your dev environment, run When I try code:
code:
|
# ? Mar 10, 2015 23:20 |
|
Biowarfare posted:is that the hilariously overengineered goonauth that requires LDAP/AD and assigns users to OUs at auth time? iirc it's laravel 4 Ah maybe that's why I'm failing. Im trying to integrate with Laravel 5...
|
# ? Mar 10, 2015 23:22 |
|
Dougie McdDouger posted:When I try That was a very very rough "getting started guide" There's more involved in getting a cloned site up and running. If you need some help, hit me up on IM and I can clone it in my dev environment and see what steps need to be taken to get it working.
|
# ? Mar 11, 2015 00:30 |
|
i would assume you didnt fill in app/config/database.php and you didnt copy app/config/goonauth-sample.php to app/config/goonauth.php and configure your LDAP server appropriately
|
# ? Mar 11, 2015 04:09 |
|
This might be overkill for someone just getting started out, but if you really want to develop a deep understanding of OOP best practices, software architecture, and the design process, I highly recommend the book Code Complete by Steve McConnell. It is easily digestible in small chunks - each chapter focuses on a different topic, and is fairly self contained, so you can learn a lot without reading the whole book from cover to cover - and it has one of the best balances of theory and nuts-and-bolts pragmatism I've seen. The examples are in Java, C++ and VB, but if you're writing OO PHP the concepts should transfer almost 100%. It's one of the best and most comprehensive "bibles" out there for software development in general and the OO approach in particular.
|
# ? Mar 11, 2015 19:41 |
|
Working on getting my PHP fundamentals solid first. But this is baffling me right now. code:
How does the program know what $flip needs to equal to echo the html? In my head it would be like this: code:
|
# ? Mar 12, 2015 20:16 |
|
What are the possible values of $flip when it's being checked inside the conditional?
|
# ? Mar 12, 2015 20:52 |
|
YO MAMA HEAD posted:What are the possible values of $flip when it's being checked inside the conditional? code:
|
# ? Mar 12, 2015 21:19 |
|
Right, so how would if ($flip) be similar to if ($flip == 1) ?
|
# ? Mar 12, 2015 22:04 |
|
I'm not sure I'm following you here sorry.
|
# ? Mar 12, 2015 22:13 |
|
Well, I realize it's because I got a little mixed up myself in trying to be didactic! You should get a general sense of http://php.net/manual/en/types.comparisons.php even if you don't memorize it; 1 is true and 0 is false, so saying if (1) is the same as saying if (1==1) because both are "truthy." This might seem convenient; it's more confusing that -1 and 42 are loosely true, or truthy. See http://php.net/manual/en/language.operators.comparison.php as well.
|
# ? Mar 12, 2015 22:28 |
|
YO MAMA HEAD posted:Well, I realize it's because I got a little mixed up myself in trying to be didactic! You should get a general sense of http://php.net/manual/en/types.comparisons.php even if you don't memorize it; 1 is true and 0 is false, so saying if (1) is the same as saying if (1==1) because both are "truthy." This might seem convenient; it's more confusing that -1 and 42 are loosely true, or truthy. See http://php.net/manual/en/language.operators.comparison.php as well. Ah. Thanks for trying though. Brilliant resources, thanks for the help.
|
# ? Mar 12, 2015 23:00 |
|
Let me explain it in a different way. Now you might be thinking that you need to put in one of these comparison operators (example: == or != or <, etc) in between the brackets of an if() to make it work. This isn't strictly true. The stuff in between the brackets is called an expression and it works like this: if() will try to convert whatever you give it to a Boolean. It will reduce the expression to true or false and if it comes up true, php will continue on by doing whatever's in the brackets. Here comes the fun part: almost anything is an expression and can therefore be reduced to a Boolean! An expression as I said, can be anything. A string, number, an array, a function, object. Exactly how and when <something> is true or false, is explained by the type comparison table. Some examples: php:<? // the simplest case is to give it a boolean directly; no need to scratch your head on this one if(true) { print 'yay'; } if(false) { print 'you will never see me'; } // numbers are easy: everything not zero is true if(5) { print 'yay'; } if(-99.534) { print 'yep, still true'; } if(0) { print 'aww.. :('; } /* How about strings? well, this is a bit more complicated. Suffice it to say that any empty string and a string with a zero in it are false, but php tries really hard to convert the string to a number and finally to a boolean, but that requires way too many words and is out of the scope of this explanation of how the if() works anyway. That comparison table is a good starting place if you want to know more, though. */ if('hello i am a bit of text') { print 'it works'; } if('0') { print 'sorry, bud'; } if('2242') { print 'but this works'; } if('false') { print 'this works too! Think about it...'; } /* arrays are false, if they are empty and that's about it */ $an_array = []; if($an_array) { print 'an empty array is useless, according to if()'; } $an_array[] = 'first value in an array'; if($an_array) { print 'aahhhhh that\'s nice'; } $an_array = [false]; if($an_array) { print 'this works too, because php doesn\'t care what\'s in it, just that _something_ is there'; } ?> Now, comparison operators and values aren't the only things you can put in an if(). Logical operators for example, or Arithmetic operators, or pretty much any operator. The key insight here is to understand that anything that returns a value, can be used. Like functions: php:<? function test() { return true; } $result = test(); if($result) { print 'yep! Test returns true, and this value is passed to the if().'; } // you don't need to use a variable though; this works too if(test()) { print 'Woah - neo'; } // like i said, any value can be used! function test2() { return []; } if(test() == false) { print 'hello!'; } if(!test()) { print 'i\'m the exact same statement as the previous one'; } ?> As a suggestion I would keep real good track of what type something is in your head, whether a number, string, array, or otherwise. There might be a lot of type juggling going on and it really pays dividends if you can immediately spot the difference between for instance '0' and 0 (when you start to work with databases you'll find that those will return everything as a string or null!). It will save you a bunch of headaches down the road because you think you're dealing with a number, but it's actually an empty array or whatever. then you can cast a variable to a type and be sure what you're dealing with. But that's for later.
|
# ? Mar 12, 2015 23:33 |
|
Awesome reply. Helped clear things up. Any tips for further learning?
|
# ? Mar 12, 2015 23:54 |
|
|
# ? Jun 4, 2024 06:23 |
Learning the difference between == and === is a nice supplement to truthy/falsy
|
|
# ? Mar 13, 2015 00:01 |