![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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(); $obj->myMethod(); Any help would be great, as well as a good tutorial on classes in PHP. Thanks |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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?
__________________
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
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();
?> |
|
|
|
|
|
#5 |
|
King of Portal
|
Your class should be as follows dark_omen:
<?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 |
|
|
|
|
|
#6 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
$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 |
|
|
|
|
|
#8 |
|
King of Portal
|
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 |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
thanks for the info
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |