Jump to content
Larry Ullman's Book Forums

Antonio Conte

Members
  • Posts

    1084
  • Joined

  • Last visited

  • Days Won

    126

Posts posted by Antonio Conte

  1. This is due to error reporting settings. You'll get an E_NOTICE warining you about "Strict standards" and calling a static method in an object context. This blog post might clarify http://www.lornajane.net/posts/2010/declaring-static-methods-in-php

     

    For the second question, you need to read the part about visibility modifiers one more time. A protected method/property is only callable within the declaring class and classes that inherits from it internally. You cannot call a protected method via application code no matter why.

     

    To clarify, you wouldn't be able to use Pet::getCount(); when the modifier is protected neither.

    <?php
    
    class Pet
    {
        protected $name;
        private static $_count = 0;
    
        function __construct( $pet_name )
        {
            $this->name = $pet_name;
            self::$_count++;
        }
    
        // Destructor decrements the counter:
        function __destruct()
        {
            self::$_count--;
        }
    
        // Static method for returning the counter:
        protected static function getCount()
        {
            return self::$_count;
        }
    
    }
    
    // End of Pet class.
    
    class Dog extends Pet
    {
        public static function count()
        {
            return parent::getCount(); // Or Dog::getCount(); Both works.
        }
    }
    
    // Create a dog:
    $dog = new Dog('Old Yeller');
    echo Dog::count();
    // echo Pet::count();           Not allowed. Pet does not have count()!
    // echo Pet::getCount();        Not allowed, protected method!
    // echo Dog::getCount();        Not allowed, protected method!
    
  2. It's a little bit hard to understand the difference. I really like the way one of my teachers explained it.

     

    When you assign something to a variable, a memory slot is reserved for the piece of information. Think of it as a drawer in a cabinet. Each drawer is labeled, and inside you find the data.

     

    - When you pass by reference, you are sending the whole drawer and it's content. Any change to the drawer's contents will change it for anyone knowing the drawer. When you pass by reference, you are referencing the slot in memory.

    - When you pass by value, you are taking out the content and sending it along. You basically clone or copy the information. If you change the content, the original will be preserved in memory. Only the copy is changed.

     

    In terms of real data, if you pass an array by value, the array is copied. When you pass by reference, you are changing the original.

     

    Scope is not really important here. The way the data is passed is what's important. JavaScript is a little strange in that way, so it's not a great language to understand the principle between the two. You can read a Stack Overflow about it here: http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language

  3. I think almost anything that's supposed to be a HTTP GET request both could and should have visible IDs. There's nothing wrong, in theory, getting a user profile by a call to profile.php?id=X. The same holds true for any actions that should be publicly available to do against a user. message.php?user_id=X is totally OK in my book.

     

    That said, editing information or permission restricted actions is another case. That's pretty obvious though.

     

    To talk concretely, I would say adding a user to a private list through a ID GET param is a good solution. As long as the user should be able to add the other, there's no need to add anything to the mystery. Other measures could be applied here.

    - Can you disable other users adding you to their lists?

    - Should there be a limit to the number of added users?

    - A maximum number of friend request per minute/hour/day?

    - Can users only add certain types of other users?

     

    All of the above should be quite simple to implement checks for. Those can be real restrictions. Using "invisible IDs" are security through obscurity. It doesn't work.

     

    To talk consequences, is the application "hacked" if a user manages to add the "wrong" user though bad input? A user is allowed to add user ID 15, but not if it's written "ABC" as input? There's no security concern there.

  4. I've moved a lot of sites over the years, and most problems seems to come from php.ini settings. It's a huge pain in the butt until you figure it out. My last longstanding problem was due to bad Apache config from hosting provider though, so you never know. I usually just throw dirt at the wall and see what sticks. No method to it for me. :P

     

    Glad you got it sorted.

  5. Take another look at your link. You've probably looked at it for too long to notice the obvious error. You are not looking for activate.php, but for activate.php12. (Where 12 would be the last inserted id.) My guess is that the link should be .php?x=12&y=MD5().

     

    A good general tip is to use very simple debugging steps for problems like this. Don't assume $x or $y holds the correct value, print them to screen along the way. Apply a print_r() to $_GET to make sure you get the correct data, and stick some simple echo statements inside your IF/ELSE-statements to follow the flow of logic. The simple ways are often the best in the end.

     

    Good luck. Also, please consider sharing the answer if you find it. It might benefit the next guy with a similar problem. :)

    • Upvote 2
  6. No question is stupid. Everyone has to start out somewhere.

     

    The script is actually just for testing your encoding. If you can see 'Iñtërnâtiônàlizætiøn', that means your encoding is correct. If you used an older encoding - like ANSI - you would see weird characters instead of the text. The encoding basically represents your "alphabet" and characters to use. The old ANSI does not support the characters in that text. That's the whole point, really.

     

    Does that make sense?

  7. We have very little to go on here, Adrian. No code, no link (even relative for your site) or other info needed to help you out. It will be purely guesswork.

     

    To actually guess, I would double check your assumptions are true. Is the file there? Is it Chmodded like the rest of your files (You can often check this with FTP - try right clicking the file and look for CHMOD/Permissions) and does the link actually point to the directory the file resides in?

     

    If non of that does help, please provide some more info. ;)

  8. I came to think about this thread once I finished writing the following code. Haha! :D

    $productId = isset($_POST['sku']) ? $_POST['sku'] : isset($_GET['sku']) ? $_GET['sku'] : false;
    

    What do you guys think? Is nesting ternary operators ALWAYS bad or would you let something like that slide? To make a case for the line, an if/else statement would take way more space, and take focus away from the more important code.

     

    It will be interesting to see what you guys think.

  9. Welcome to the forums. Keep in mind that lots of error sources can be searched for. This error is a common one, and happens because PHP can't find config.inc.php at the path you specified. Make sure the file exists and that the path to it is actually correct.

     

    The last fatal error is because of PHP's include path. If a file is NOT found at include path, PHP will search in a folder named by the constant include_path. You can mostly ignore the last part and focus on the main error.

    • Upvote 1
  10. One of the caveats of Larry's ability to translate Geek into English is that some of the geek gets left out. (willingly) Way to many writers are too hung up in definitions and technical phrasing that their explanations gets unnecessary complicated. Don't get to hung up in that the phrasing might be slightly different than the common technical terminology. That is often what drives the point true.

     

    ----

     

    2. You are correct about $this. You can choose to skip this.property or this.method() in both Java and C#. The same applies to constants and static methods and members. I would've liked to be able to leave them out myself, as $this and self:: are mostly noise words in your code anyway. I hope they become optional some day.

     

    3. I think they did this to explicitly separate PHP 4 and PHP 4.3 object-orientation. At that time, PHP did not even have access modifiers (public, protected, etc) and I think they did things a little differently for compatibility reasons between versions. That is what I recall to have read sometime at least.

  11. I demand "voice-to-code" functionality, Larry. And I demand it for early next week!

     

     

    Jon: You can often find your post in the forum cache. There's a "restore post" option somewhere. My workflow is always to press CMD + A and CMD + C to save the post before positing. You are the problem here, old man!

     

    :P

     

    If anyone didn't get that, I'm only joking of course.

     

    • Upvote 1
  12. You need to follow these steps to implements such functionality:

     

    1. Keep track of views. This can be saved in your post/article/etc table along with other info, or in a separate table with a foreign key to the article ID. Each time an article is loaded using PHP, simply update the views column in the table. (You might also use flat-files, the principle is the same)

     

    2. Query for articles based on views. The basic query looks something like this:

    SELECT * FROM articles ORDER BY views DESC LIMIT 10
    • Upvote 2
  13. I also love ternary. My "rule" is that I replace a ternary with a normal IF-statement when the logic is pushed to far right. Legibility is often the most important for me, so I keep that in mind. I also love the "replace logic with query" refactoring, and use that often for legibility both in if-statements and when using the ternary operator:

    public function something( $value )
    {
       if ( self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT )
       {
          return self::SOME_CONTANT_VALUE;
       }
    
       return self::OTHER_CONSTANT_VALUE
    }
    
    // Too long Ternary
    public function something( $value )
    {
       return ( self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT ) ? self::CONSTANT : self::OTHER_CONSTANT;
    }
    
    // Refactoring
    
    public function something( $value )
    {
       return ( $this->expressiveMethod($value) ? self::CONSTANT_VALUE : self::OTHER_CONSTANT;
    }
    
    private function expressiveMethod( $value )
    {
       return self::SOME_MINIMUM_CONSTANT < $value && $value < self::SOME_MAXIMUM_CONSTANT )
    }
    
    • Upvote 1
  14. They are both multidimensional arrays, yes. There's a big difference on how you structure your array, though. You assign each value for a single student to three keys in the base arrays. They are thus not logically connected to each other. You basically create three separate value arrays assigned to $arr[0 -> 2] instead of creating three students in $arr[0 -> 2].

     

    The array you create would look like the following in "Larry's style":

    $arr = array(
       0 => array(256, 2, 9),
       1 => array("Jon", "Vance", "Stephan"),
       2 => array(98.5, 85.1, 94.0)
    );
    

    A structure more comparable to the example would be to assign the array like this:

    $ar[0][0] = 256;
    $ar[0][1] = "Jon";
    $ar[0][2] = 98.5;
    
    $ar[1][0] = 2;
    $ar[1][1] = "Vance";
    $ar[1][2] = 85.1;
    
    $ar[1][0] = 9;
    $ar[1][1] = "Stephan";
    $ar[1][2] = 94.0;

    That structure would be equal to:

    $arr = array(
       0 => array(256, "Jon", 98.5),
       1 => array(2, "Vance", 85.1),
       2 => array(9, "Stephan", 94.0)
    );
    

    Keep in mind that the version is still not comparable to Larry's example. The difference is the indexing of the array. Both versions above are based on a standard zero-to-Nth indexes, while Larry indexes according to the student numbers. To talk about why he might want to do that, the foreach-structure allows for mapping arrays to KEY => VALUE pars. You could thus ouput the array like this:

    foreach ( $array as $studentNumber => $student )
    {
       echo "{$studentNumber}: {$student[1]}"; // Student[1] === "the name".
    }
    
    // But, since we have student number in the array above, we could get it that way:
    foreach ( $array as $student )
    {
       echo "{$student[0]}: {$student[1]}"; // S[0]: "the Stud.num" — s[1] === "the name".
    }
    

    The point of grouping all student info together inside a second array (as explained, you build three value arrays now) is that your sorting might mix the key together. If you sort the grades, the names will be out of sync in your version. "Jon" might therefor get a credit of 85.1 instead of the 98.5 he deserves... And that would be bad. ;)

     

    If you really prefer your version, and have at least PHP 5.4 installed, you can now also use a more declarative array syntax. Without a version >= PHP 5.4, you'd simply replace [V, ... ,V] with array(V, ... ,V). I would suggest you do something along the lines of:

    // Zero-to-Nth key mapping
    $arr[0] = [256, "Jon", 98.5];
    $arr[1] = |2, "Vance", 85.1];
    $arr[2] = [9, "Stephan", 94.0];
    
    // Student num key mapping
    $arr[256] = ["Jon", 98.5];
    $arr[2]   = ["Vance", 85.1];
    $arr[9]   = ["Stephan", 94.0];
    
    // Without PHP 5.4
    $arr[256] = array("Jon", 98.5);
    

    The next point is associate keys versus integer mapping. In other languages, such as Java, C# or regular C++ (which PHP is largely based on) normal arrays MUST be mapped to integers. If you want to assign values to a key like "name", ("name" => "Jon") you will need to use a structure called a hash map. PHP simplifies this by allowing both Strings, integers and other primitive types to be used as keys in the same array. The point of this is to simplify the structure a bit.

     

    We could thus assign the student array as $arr[N] = ["name" => ..., "grade" => ...  ]; This will look like the following:

    $arr[256] = ["name"=> "Jon", "grade" => 98.5];
    $arr[2]   = ["name"=>"Vance", "grade" => 85.1];
    $arr[9]   = ["name"=>"Stephan", "grade" => 94.0];
    

    When we foreach that array, we can thus get the values this way:

    foreach ( $array as $studentNum => $student )
    {
       echo "#{$studentNum}: {$student['name']} ({$student['grade']})"; 
       // First iteration of $arr output: #256: Jon (98.5)
    }

    Hope that clears up some confusion.

    • Upvote 1
  15. I do that myself. You could possible use Strings too, but I wouldn't like the need for conversions. As stated, when it comes to money, you'd want as few possibilities for errors as possible.

     

    I'm also currently using an unimmutable Money class that I trust for interactions with Stripe. That way, I can perform addition and subtraction (Vouchers, shipping, summing totals for products, etc) in a Checkout object instead of using changable primitive types. The only place I use the amount as an integer is in a Stripe class that handles the actual payments. That way, what I put in stays in.

     

    You you read about Fowler's Money Pattern before you do anything else. You'd want to encapsulate both the amount and a Currency instead of using primitive integers in PHP.

    • Upvote 1
×
×
  • Create New...