Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2006, 3:26 PM   #1
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
PHP class

I made a php class, but how do I use it?
I know that you make an object using the constructor:
$obj = new ClassName();
And I know that you can use functions of the class by:
$obj->myMethod();
but for some reason it gives me an error. Is there something I am missing (btw all I did was make the object in the other php file)?

Any help would be great, as well as a good tutorial on classes in PHP.
Thanks
dark_omen is offline   Reply With Quote
Old Jul 12th, 2006, 3:29 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
1. We need to see your source to pin point your problem, that pseudo code doesn't tell us much.
2. What version of PHP?
__________________

tempest is offline   Reply With Quote
Old Jul 12th, 2006, 3:41 PM   #3
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
Well here it is, but it's not much different from what I posted before.
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
</head>
<?php
$obj = new myClass();
echo $obj->write();
?>
<body>
</body>
</html>
[/php]

And this is the class:
[php]
<?php
class myClass{
var $h;

//constructor
function myClass(){
$h = "Hello";
}

function write(){
return $h;
}
}
?>
[/php]

And I am writing it in php 5.1.4
dark_omen is offline   Reply With Quote
Old Jul 12th, 2006, 3:52 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Try using include_once() or require_once() to put your class definition in the file where it's being used.

<?php
   include_once("myclass.php"); // or whatever
   $obj = new MyClass;
   $obj->write();
?>
Jimbo is offline   Reply With Quote
Old Jul 12th, 2006, 5:22 PM   #5
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Your class should be as follows dark_omen:
<?php
class myClass{
    var $h;

    //constructor
    function __construct(){
        $this->$h = "Hello";
    }
    
    function write(){
        return $this->$h;
    }
}
?>
So basically for any instance field of the class or when you want to return a value which uses an instance field in its computations, you have to specify $this-> in your code. Also, your constructor for the class is always named __construct. And that should make your class work as you want it to.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jul 12th, 2006, 5:36 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by grimpirate
Also, your constructor for the class is always named __construct. And that should make your class work as you want it to.
Pre-PHP 5, it was the same name as the class, and it should be backwards compatible. But, as this is being done in PHP >= 5, __construct is correct.
Jimbo is offline   Reply With Quote
Old Jul 12th, 2006, 6:20 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
$this->$h should be $this->h. I believe his biggest problem was not having the class definition available in the file where he used it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 12th, 2006, 7:55 PM   #8
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Good looking out DaWei, I accidentally left that dollar sign in there. Here is the properly revised code.
<?php
class myClass{
    var $h;

    //constructor
    function __construct(){
        $this->h = "Hello";
    }
    
    function write(){
        return $this->h;
    }
}
?>
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Jul 12th, 2006, 8:58 PM   #9
dark_omen
Hobbyist Programmer
 
Join Date: Dec 2004
Posts: 125
Rep Power: 4 dark_omen is on a distinguished road
Send a message via AIM to dark_omen
thanks for the info
dark_omen is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
URL class Eric the Red Java 5 Jun 24th, 2006 9:01 PM
using classes in another class ling_wong C++ 6 Jan 23rd, 2006 9:55 PM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 8:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC