Jump to content

[SOLVED] The things I not understand about php


chaiwei

Recommended Posts

Hi all,

 

Please shed some lights on the following questions.

Sorry for the bad arrangement. Thanks.

 

1. What is the  :: means?

// Load settings from parent class
$settings = SystemComponent::getSettings();

 

2.What is this  &$this  ? A variable? why in front got the  &  ?

 

register_shutdown_function(array(&$this, 'close'));

 

why the register_shutdown_function() put the array(&$this, 'close') inside??

why don't we put

 register_shutdown_function('close');  

 

3.

Scripts tend to die, and that’s not usually nice. We do not want to show the user a fatal error nor a blank page (display errors off) . PHP has a function called register_shutdown_function which lets us set up a function which is called at execution shutdown.

The execution shutdown means when the time the script encounter the error, it will call the function??

 

example like,

mysql_connect($host,$user,$pass) or die();

 

instead of use die(),

we use :

mysql_connect($host,$user,$pass) or 
register_shutdown_function(array($this, "cant_connect"), "mysql_error"); 

function cant_connect($error="err"){
  echo $error;
}

So the output will be :

mysql_error

 

Correct?

 

 

4

get_magic_quotes_gpc() 

    If it is on, means all the double and single quotes which contain in the GET,POST,COOKIE are already escaped with the addslashes ??

example, $name=david's

So if we still addslashes when it is on it will  become david\'s right?

Because when the time the system detect there is a 's it will automatically addslashes become 's

but if we still addslashes on it so it become \'s and the system detect there got another slashes,

and it automatically addslashes on it, so it become 3 slashes right?

 

The system only detect the value which use POST,GET,COOKIE method only? by using the magic_quotes_gpc() ?

 

How about the initialize in variable?

$name="david's"  <<<it is not in get,post, and cookie right?

so the system wouldn't addslashes on it no matter the magic_quotes_gpc is ON or OFF.

 

output will still be david's right?

 

Please point out my mistake.

5.

print <<< EOF
abc abc abc
EOF;

 

output: abc abc abc

 

what is the different with echo?

 

6.

stripslashes(addslashes("david's"));

return original values right?

what if:

addslashes(stripslashes("david's"));

return      david's    correct?

 

 

Link to comment
Share on other sites

1) The :: means call the class's static member function (and a few other things). It is dubbed the "Scope Resolution Operator"

http://us2.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

 

2) The & means to create a reference. ( http://us2.php.net/references )

 

$this is a variable that refers to the class the function is being called in. The register_shutdown_function() must know which class you're registering (see #3), so you pass it a reference to $this so it can do its job. If you're using a global function (function outside of a class), you don't need to pass it an array, just 'close'.

 

All objects are passed by reference as of php5 though, so you do not really need the & in that instance (if you use php 5 of course).

 

3) The function you pass to register_shutdown_function() is called whenever a script completes its execution... it's really not as useful as some of the error handling functions for stopping users from seeing errors. http://us3.php.net/register_shutdown_function

 

As for your example.... no. *Edit: actually, yes.. I didn't really look at it much at first. But there certainly are better ways of doing it than like that.

 

4) You're correct, it only adds backslashes to the variables sent by the user. eg. with mypage.php?name=david's the variable $_GET['name'] will contain david\'s (this is just GET, it also does it to POST and COOKIE as you said). It does not apply it to variables you assign in your script's execution. http://us3.php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

 

5) That's HEREDOC syntax. http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc it also works for echo (and anything else that uses a string)... in fact, print and echo are 100% identical, they do the same thing.

 

6)

<?php
stripslashes(addslashes("david's")); // david's
addslashes(stripslashes("david's")); // david\'s
?>

 

stripslashes() undoes what addslashes() does (as well as the magic_quotes_gpc you were talking about earlier), and not the other way around.

 

run this and try to figure out why it works the way it does:

 

<?php
echo stripslashes(addslashes("david's");
echo addslashes(stripslashes("david's");
echo stripslashes("david's");
echo addslashes("david's");
echo stripslashes("david\'s");
echo addslashes("david\'s");
?>

 

Link to comment
Share on other sites

Hi,

 

Thanks a lot for the information.

I will look deep into it.

Thanks a lot.

 

Another question for the class constructor.

 

Some of the class they use: example

class ABC{

  function __construct(){   //which 1 is the real constructor?  __construct() or ABC()
   return 'thank you';
  }

    function ABC(){           //basically it will run the __construct() first right? then only run the ABC() ??
      return 'thanks';         //I mean when >>>>   echo new ABC(); 
    }                                 // Is it correct to echo like this? >>  echo new ABC(); 
}




Link to comment
Share on other sites

I figured he was just curious about things he saw on someone's website somewhere, I for one like the multiple-easily-answered-questions form of question asking. I'm sure he didn't have this as a homework assignment (Who WOULD assign this as homework?).

 

php 4 uses the class's name as the contructor, php 5 uses __construct(). I'm sure the php 4 version would work in php 5.

 

edit: oh, and thanks for the correction on print/echo.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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