Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
After several online searches, I couldn't find an answer to this.

When I do this:
code:
<?php
echo include("/pages/what-s-happening.page.php");
?>
a 1 appears after the block of text that's printed. I've seen this before when doing something like print_r($array), but never with using echo and I'm unsure of what this means. The file I'm including is just HTML coding for a page:
code:
<p>text</p>
so I'd see:
code:
<p>text</p>1
and since nothing ever mentions this, I'm confused about it and how to make it so that 1 doesn't appear.

e: fixed it by changing it to get rid of the echo, but an explanation of the 1 would still be appreciated.

Master_Odin fucked around with this message at 19:20 on Jul 3, 2010

Adbot
ADBOT LOVES YOU

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
So recently, an old client contacted me today saying that he hosed up an upload script I wrote a while ago that used the APC library to create an upload bar. He apparently managed to delete APC from the server among other things. Anyway, managed to get it installed (or at least it looks installed), yet the bar isn't working and I'm not sure as it was about 2 and 1/2 years ago that I wrote this and I honestly don't remember the APC library well enough to actually debug this and it's pretty mission critical. Hopefully you goons can help me.

My scripts:
http://pastebin.com/vd4T0zHF (index html)
http://pastebin.com/FHMWPxiZ (getprogress.php)
http://pastebin.com/XW0Msvk0 (apc settings)

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

McGlockenshire posted:

The code looks correct at first glance.

Keep in mind that APC upload progress only works under mod_php. If you're using FastCGI, you're out of luck.
So I think I got it switched over to mod_php (it is available for the newest version of PHP right? I'm really rusty at this stuff) except I get the error:
Warning: session_start() [function.session-start]: open(/tmp/sess_8e01f9802198750c4bc92fa1c8c7c24a, O_RDWR) failed: Permission denied (13) in /home/client/public_html/xsuploader/index.php on line 2

I set /tmp in the linux root to 777 as well as the tmp in /home/client yet I'm still getting that error. It showed up when I set the PHP configuration to use DSO instead of SuPHP. Turning suExec on/off didn't seem to make a difference.

Is there another easy upload bar that I could use as it seems like installing APC has become a headache in and of itself.

e: on Chrome I get 0% before it successfully uploads and on firefox I get 1% before it uploads successfully on a 8mb upload.

Master_Odin fucked around with this message at 17:46 on Jul 21, 2011

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

McGlockenshire posted:

Just to hone in on the APC issue, suPHP runs PHP as a CGI, not under mod_php.

With regard to the session issue, are you running RHEL/CentOS? If so, is SELinux enabled? Check /var/log/audit/audit.log. Apache is probably forbidden from writing to /tmp.
The server is running CentOS 5.6. I don't know about RHEL. I have no idea what I'm looking at with /var/log/audit/audit.log. SELinux was lacking a file "enforce" in the directory so I created one and put 0 in it to make sure it was off and not affecting things. I know the server has to be set to DSO to get Apache2 to handle the PHP scripts. It's just I have no idea how to deal with permissions besides CHMOD because I'm a god drat programmer, not a server admin :smithicide:

e: I'm probably just going to relay this stuff to my client and tell him to contact his server provider to deal with this because gently caress trying to figure this poo poo out as I don't want to try and learn how to use/manage a server like this.

Master_Odin fucked around with this message at 04:08 on Jul 22, 2011

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
Alternatively, you can use array_splice. The primary downside is that you have to run a final splice outside the loop to pick up the final group.

php:
<?
$groups = array();
$check = 0;
$last = 0;
$count = 0;
foreach($a as $k => $v) {
    $check += $v;
    if ($check >= 100) {
        $groups[] = array_slice($a,$last,($count-$last));
        $last = $count;
        $check = $v;
    }
    $count++;
}
$groups[] = array_slice($a,$last,($count-$last));
?>

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

Vedder posted:

That's exactly what I need and thank you for introducing me to PDO, just read a couple of tutorials and I will have a go later. I always find PHP is one of those languages where some people work completely different to other people. Sometimes you will see a project that is a mixture of code and HTML, other times it's pure OO.

My background is a Network Admin that has used PHP over the years, but I am getting into it again with a few projects at work, if you or anyone in this thread could recommend a bang up to date PHP book so I can catch up?

Thanks
php.net is the best reference as it's as up-to-date as you can get, and also talks about things that are depreciated within the language. For example, they have a big ol' warning about how don't use mysql_fetch_array as it's being dropped and use PDO (or mysqli) instead. My general cycle for trying to figure something out within PHP is look up kind of what I want to do, find out what functions other people use, look up those functions on php.net to see how to use it, any warnings, etc.

If you're set on getting a book, I'd only look for one that goes into design paradigms (like when/how to use singleton classes, factories, etc.) as that's a bit harder to pickup than language features.

Master_Odin fucked around with this message at 16:13 on Jul 29, 2013

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
You want to use pagination or something where you have
code:

$i = (isset ($_GET['i'])) ? intval($_GET['i']) : 1;
// get api
header('Location: index.php?i='. ($i+1));

You'd also want to use 'a' in the fopen if using the same file.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

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.
There are cases where you have to have if/switch statements without some "cleaner" way to do it. For example it's useful when you'd have a bunch of if statements would only check for equality. This is common when you're dealing with types and you have to manipulate data depending on the type (which might include calling a specific function for that specific type). An example of this, I mean that your incoming data is a string, and it could represent a string, a boolean, a number, a date, etc.

The last switch statement I wrote was given a database info object, open an appropriate table factory for it. I could have written

code:
if (dbInfo.getType() == DBTypes.ORACLE) {
    // do stuff
}
else if (dbInfo.getType() == DBTypes.POSTGRES) {
    // do stuff
}
...
but it was cleaner (imo) to just make a switch:
code:
switch(dbInfo.getType()) {
    case ORACLE:
        // do stuff
        break;
    case POSTGRES:
        // do stuff
        break;
    ...
}
You definitely couldn't accomplish this through a function/class and need to rely on an if or a switch. I'm not sure why you'd say the switch is some horrible thing over the if.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

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.

That's not to say you should never use switch statements - writing a ladder of if statements because "switch statements are bad :downs:" 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.
Oh yeah, if I was littering the code base with these checks sure, but there's one of these in the code base, I'm not sure creating an interface/subclasses with one function is quite worth it yet as what problem is that really fixing? You'd still just be looking at one function instead of some other function.

How about :
code:
// data = is a string coming from user input
// field is a generic container which can hold one thing of any type
switch(toType) {
    case STRING:
        field.setString(data);
        break;
    case INTEGER:
        field.setInteger(data);
        break;
    case DOUBLE:
        field.setDouble(data);
        break;
    case DATE:
        field.setDate(data);
        break;
    ...
}
How would you solve that with polymorphism?

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

revmoo posted:

Oh, we're doing JS in the php thread now? :)

code:
field.setData = function(toType, data) {
    if (toType == 'STRING') this.setString(data);
    if (toType == 'INTEGER') this.setInteger(data);
    if (toType == 'DOUBLE') this.setDouble(data);
    if (toType == 'DATE') this.setDate(data);
}
field.setData(toType, data);
Excuse any glaring syntactical issues I haven't had my morning coffee yet. The reason I'd do it this way is because you can jam a ton of various conditions into the function and still fit them on a single page. Switch statements are harder to read. Notice how all the conditions are stacked right on top of each other line after line? This solution is half the line count also.
I'm going to assume you meant to use else ifs and not just if statements?

It was actually Java, but you could apply the same ideas to PHP with constants:
code:
// $data = is a string coming from user input
// $field is a generic container which can hold one thing of any type
switch($toType) {
    case Types::STRING:
        field.setString($data);
        break;
    case Types::INTEGER:
        field.setInteger($data);
        break;
    case Types::DOUBLE:
        field.setDouble($data);
        break;
    case Types::DATE:
        field.setDate($data);
        break;
    ...
}
I personally hate the way you've written your if statements and think that the switch is cleaner to read overall. It's a style thing really, and the same argument of "I don't see any good use cases" for shooting down ever using a switch could be applied to any other style thing (like tabs/spaces, etc.).

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

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.

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.
It's more efficient to use a switch in compiled languages and will give you some runtime speed improvement. Especially if you have 150+ conditionals.

http://stackoverflow.com/questions/395618/is-there-any-significant-difference-between-using-if-else-and-switch-case-in-c

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
What's the best way to unit test a database driver class with phpunit? It uses PDO to connect to a database and then gives us utility functions to the underlying PDO class. Would I always want to hook this up to simple database and straight unit test this or is it generally better to mock the PDO class (and how? Pass in the PDO class to be used?) and then just mock out the necessary functions as necessary?

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
You're probably looking for Doctrine.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

cheese-cube posted:

Speaking of installing PHP, is the PPA from Ondřej Surư considered reputable: https://launchpad.net/~ondrej/+archive/ubuntu/php? I added it quite some time ago to get the latest PHP back when Ubuntu and Debian repos were lagging behind the times and its been pretty good I guess.
Yeah, his work is the definition of a reputable PPA you don't need to worry about installing on your server.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
The main problem here is the you're looking at the source code of the latest version of the Inflector which laravel does not use, which is why you can't find what the concrete WordInflector it's using. A quick look at it's composer.json reveals it has a ^1.1.0 dependency on the Inflector, so you could start there (or at 1.3, you'd have to look at the composer.lock file to get the exact version they're assuming).

Looking at the 1.3 release (though I'm not fully sure it uses this, or an older version still, you'd have to check) should make more sense: https://github.com/doctrine/inflector/blob/1.3.x/lib/Doctrine/Common/Inflector/Inflector.php

It should be noted that the new version of the Inflector that is being designed does not have static methods similar to the old version which would be a clue you weren't looking at the right code.

Master_Odin fucked around with this message at 04:30 on Apr 12, 2019

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
That's an IDE thing, PHP does not support named parameters unfortunately.

PHPStorm refers to it as inlay hints.

Adbot
ADBOT LOVES YOU

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

22 Eargesplitten posted:

Are there any major security issues with the latest version of PHP 7.0? I see that 7.2 exists, but I'm not sure if the current software I'm running will work with it, and I'd rather not risk breaking anything if there's no security issues. I can find security flaws doing a search on Google but I don't know which have been patched and which are still out there.

https://github.com/PHPCompatibility/PHPCompatibility is a good library to help alleviate some of the pain of upgrading PHP versions.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply