Current location: Hot Scripts Forums » Programming Languages » PHP » Classes


Classes

Reply
  #1 (permalink)  
Old 05-25-08, 04:09 PM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Classes

i would like to know, is this syntax correct?

PHP Code:

<?php

//test.php

class test
{
    public function 
test()
    {
        require_once(
'test2.php');
        new 
test2();
    }
}

new 
test();

?>

<?php

// test2.php

class test2
{
    public function 
test2()
    {
        echo 
'hello world';
    }
}


?>

Last edited by UnrealEd; 05-26-08 at 02:34 AM. Reason: please use the [php] wrapper when posting php code
Reply With Quote
  #2 (permalink)  
Old 05-25-08, 05:28 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
You have: new test2();
You need to put this into a variable or return it: $object = new test2();

Are you wanting to have test1 connect to test2?
(Untested)
PHP Code:

<?php


//test.php

class test extends test2
{
    public function 
__construct()
    {
        
parent::__construct() ;    
    }
}

require_once(
'test2.php');
$object = new test();

?>

<?php

// test2.php

class test2
{
    public function 
__construct()
    {
        echo 
'hello world';
    }
}


?>
Notice im using __construct() instead of the class name...
This is a php5 thing: http://nz2.php.net/manual/en/language.oop5.php

Any questions?
Lex
__________________
01010000 01001000 01010000
Reply With Quote
  #3 (permalink)  
Old 05-25-08, 07:30 PM
xportal xportal is offline
Newbie Coder
 
Join Date: Oct 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by phpdoctor View Post
You have: new test2();
You need to put this into a variable or return it: $object = new test2();

Are you wanting to have test1 connect to test2?
(Untested)
PHP Code:

<?php


//test.php

class test extends test2
{
    public function 
__construct()
    {
        
parent::__construct() ;    
    }
}

require_once(
'test2.php');
$object = new test();

?>

<?php

// test2.php

class test2
{
    public function 
__construct()
    {
        echo 
'hello world';
    }
}


?>
Notice im using __construct() instead of the class name...
This is a php5 thing: http://nz2.php.net/manual/en/language.oop5.php

Any questions?
Lex
nope i'm not trying to extend the class. i was under the impression that i could not have a class written and instantiate inside a method of another class, but i tested the code and i didnt get any errors, i just wanted to know if the syntax was correct, also i dont necessarily want to use it in the __construct

function i just used the class name same as the function as the example. thanks though

Last edited by xportal; 05-25-08 at 07:37 PM.
Reply With Quote
  #4 (permalink)  
Old 05-26-08, 12:53 AM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Oh ok, well the syntax is correct ye.
The new test2(); should something like this: $object = new test2();
so you can use the object... The __construct is php5 only also.

But ye, your syntax is all good mate.

Lex
__________________
01010000 01001000 01010000
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes and Timer Jay6390 Visual Basic 2 12-24-07 06:44 AM
Girl with no clue!!!!Booking Classes PHPMYSQL jobless1 Script Requests 0 07-13-07 03:56 AM
Classes and Variables Sabu PHP 5 07-03-05 08:49 PM
Use Graphics classes in a class library RFickling Windows .NET Programming 0 01-15-05 02:07 PM
Free set of classes for 3D coordinate manipulation? Hjalmarsson Everything Java 0 10-30-03 07:59 AM


All times are GMT -5. The time now is 07:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.