php-general Digest 20 Apr 2009 03:15:48 -0000 Issue 6076

Topics (messages 291666 through 291670):

self in inherited methods
        291666 by: Alex S Kurilo aka Kamazee

How do I access a local variable?
        291667 by: abdulazeez alugo
        291668 by: Chris
        291669 by: Paul M Foster

DATE / strtotime
        291670 by: Ron Piggott

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Is it right that 'self' in inherited method still points to the parent?
If it is, can you explain it? It makes me worry :)

A piece of code below for example

<?php
class MyParent {
       const NAME = 'MyParent';
       public function get_instance() {
               return new self;
       }
       public function get_another_instance() {
               $class_name = get_class($this);
               return new $class_name;
       }
       public function get_name() {
               return self::NAME;
       }
}

class MyClass extends MyParent {
       const NAME = 'MyClass';
}

$a = new MyClass;
$b = $a->get_instance();
$c = $a->get_another_instance();

echo $a->get_name(),"\n";
echo get_class($b),"\n";
echo get_class($c),"\n";
?>



--- End Message ---
--- Begin Message ---
Hi guys,

I have a function inside which I set alocal variable to store a result. Can I 
access this variable in another function? if yes then how?

function tbl1($entrytitle, $entrytext)

{

global $conn;

$result= mysql_query("INSERT INTO tbl1(tbl1_id, title, text)

     VALUES('NULL', '$entrytitle', '$entrytext')", $conn);

     $tbl_id=mysql_insert_id($conn);// this is the local variable I'm setting 
to get the last auto increment id

} 

 

Now I wish to access that variable in another function thus:

function tbl2($name, $entrytitle, $entrytext)

{

global $conn;

$result =mysql_query("INSERT INTO tbl2(tbl1_id, name, title, text)

                               VALUES('$tbl1_id', '$name', '$entrytitle', 
'$entrytext' )", $Conn); 

}

 

Or is there a better way to store the variable?

Thanks in advance.

 

Alugo Abdulazeez

www.frangeovic.com

_________________________________________________________________
More than messages–check out the rest of the Windows Live™.
http://www.microsoft.com/windows/windowslive/

--- End Message ---
--- Begin Message ---
abdulazeez alugo wrote:
Hi guys,

I have a function inside which I set alocal variable to store a result. Can I 
access this variable in another function? if yes then how?

No, you can't. You either need to pass it back (recommended) or make it global (not recommended).

function tbl1($entrytitle, $entrytext)

{

global $conn;

$result= mysql_query("INSERT INTO tbl1(tbl1_id, title, text)

     VALUES('NULL', '$entrytitle', '$entrytext')", $conn);

     $tbl_id=mysql_insert_id($conn);// this is the local variable I'm setting 
to get the last auto increment id

    return $tbl_id;

}
Now I wish to access that variable in another function thus:

function tbl2($name, $entrytitle, $entrytext)

{

  $other_id = tbl1($entrytitle, $entrytext);
  echo $other_id;

global $conn;

$result =mysql_query("INSERT INTO tbl2(tbl1_id, name, title, text)

VALUES('$tbl1_id', '$name', '$entrytitle', '$entrytext' )", $Conn);
}

PS : look up mysql_real_escape_string for when you save your data.

--
Postgresql & php tutorials
http://www.designmagick.com/


--- End Message ---
--- Begin Message ---
On Mon, Apr 20, 2009 at 12:54:27AM +0100, abdulazeez alugo wrote:

> 
> Hi guys,
> 
> I have a function inside which I set alocal variable to store a result. Can I 
> access this variable in another function? if yes then how?
> 
> function tbl1($entrytitle, $entrytext)
> 
> {
> 
> global $conn;
> 
> $result= mysql_query("INSERT INTO tbl1(tbl1_id, title, text)
> 
>      VALUES('NULL', '$entrytitle', '$entrytext')", $conn);
> 
>      $tbl_id=mysql_insert_id($conn);// this is the local variable I'm setting 
> to get the last auto increment id
> 
> } 
> 
>  
> 
> Now I wish to access that variable in another function thus:
> 
> function tbl2($name, $entrytitle, $entrytext)
> 
> {
> 
> global $conn;
> 
> $result =mysql_query("INSERT INTO tbl2(tbl1_id, name, title, text)
> 
>                                VALUES('$tbl1_id', '$name', '$entrytitle', 
> '$entrytext' )", $Conn); 
> 
> }
> 
>  
> 
> Or is there a better way to store the variable?

If a variable is local to a function, there is no way to access that
variable outside that function. You can pass it back as a return value
from the original function, or make it global (not advised) to make it
visible in other functions.

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
Where $date_reference is 2009-04-18 the following code gives me a day of
1969-12-30. How do I get it to be 2009-04-17? 

$previous_date = strtotime("-1 days", $date_reference); 
$previous_date = date('Y-m-d', $previous_date);

echo $previous_date;   outputs 1969-12-30

Ron


--- End Message ---

Reply via email to