Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   PHP class (http://www.programmingforums.org/showthread.php?t=10696)

dark_omen Jul 12th, 2006 4:26 PM

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

tempest Jul 12th, 2006 4:29 PM

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?

dark_omen Jul 12th, 2006 4:41 PM

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

Jimbo Jul 12th, 2006 4:52 PM

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();
?>


grimpirate Jul 12th, 2006 6:22 PM

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.

Jimbo Jul 12th, 2006 6:36 PM

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.

DaWei Jul 12th, 2006 7:20 PM

$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.

grimpirate Jul 12th, 2006 8:55 PM

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;
    }
}
?>


dark_omen Jul 12th, 2006 9:58 PM

thanks for the info


All times are GMT -5. The time now is 12:48 AM.

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