开发者

Using class in another class easily with an example

开发者 https://www.devze.com 2023-03-09 19:30 出处:网络
I have a php class that tells time like this (Time.class.php): <?PHP class Time { var $timestamp; function timestamp () {

I have a php class that tells time like this (Time.class.php):

<?PHP
class Time {
    var $timestamp;
    function timestamp () {
    $this->timestamp = date('YmdHis');
    return $this->timestamp;
    }
}
?>

What I want to do is to call this time in a different class such as (Test.class.php):

<?PHP
class Test {
    function test (){
    $timestamp = ' '; // <--开发者_Go百科- the timestamp from the other class
    $hourago = $timestamp - 10000;
    return $hourago;
    }
}
?>

I'm new to PHP classes so I didn't understand what I've read on this subject. From what I've read this can be done with global scope (if its called a scope)?? If you could only show me how to use globals or how to solve a problem like this easily I would appreciate it...


You need to create an instance of the Time class first, then call the method on that instance.

$mytime = new Time();
$timestamp = $mytime->timestamp();

Alternately you could look at static class methods.


You must use DEPENDENCI INJECTION.

You can pass any references objects in the costructor like this:

   function test ($yourTimeStamp){
       $timestamp = $yourTimeStamp
       [...]
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消