SitePoint Sponsor |
|
User Tag List
Results 1 to 22 of 22
-
Dec 6, 2004, 01:25 #1
Newbie in oop of php,Anyone plz help me!
Hi everybody,
I am very new in object oriented in php.
plz solve me out.
I am creating a class which saved class.Demo.php
<? php
public class Demo {
public $_name;
public function sayhello() {
print "Hello ($this->getname()}!";
}
public function getname() {
return $this->_name;
}
public function setname($name) {
if(!is_string($name)||strlen($name)==0) {
throw new Exception("Invalid name value");
}
$this->_name = $name;
}
}
?>then create a file which saved as testdemo.php
<?php
require_once('class.Demo.php');
$obj = new Demo();
$obj->setname('ujjwal');
$obj->sayhallo();
$obj->setname(37);
?>If i open testdemo.php then see an error i.e
Parse error: parse error, unexpected T_STRING in d:\inetpub\wwwroot\class.Demo.php on line 2
Fatal error: Cannot instantiate non-existent class: demo in d:\inetpub\wwwroot\testdemo.php on line 3
Plz can u solved it. advanced thanks for u'r help.
-
Dec 6, 2004, 01:32 #2
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You defined your function with the name sayhello, then called it with the name sayhallo.
Last edited by vinyl-junkie; Dec 6, 2004 at 08:15. Reason: Wrong word. - frustrated grammarian ;)
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 6, 2004, 01:45 #3
Originally Posted by vinyl-junkie
-
Dec 6, 2004, 03:54 #4
- Join Date
- May 2004
- Location
- Germany
- Posts
- 550
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
use class Demo instead of public class Demo, php is not java
-
Dec 6, 2004, 04:04 #5
- Join Date
- Dec 2004
- Location
- Cornwall, UK
- Posts
- 594
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can use the public keyword in php5, but I don't think you can before a class. After all, why have a class if you can't access it?
-
Dec 6, 2004, 04:13 #6
Originally Posted by Daimaju
-
Dec 6, 2004, 04:15 #7
Originally Posted by trib4lmaniac
can u pointed this thing?
-
Dec 6, 2004, 04:23 #8
- Join Date
- Dec 2004
- Location
- Cornwall, UK
- Posts
- 594
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Don't double post, edit the previous one
What version of php are you using?
-
Dec 6, 2004, 04:43 #9
Originally Posted by trib4lmaniac
How can i remove this error?
-
Dec 6, 2004, 04:55 #10
- Join Date
- Dec 2004
- Location
- Cornwall, UK
- Posts
- 594
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Try taking out all occurences of the keyword "public".
Look on these pages for class differences between php4 and 5...
http://uk.php.net/manual/en/language.oop.php
http://uk.php.net/manual/en/language.oop5.php
-
Dec 6, 2004, 05:01 #11
- Join Date
- Oct 2003
- Location
- The Netherlands
- Posts
- 406
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by trib4lmaniac
PHP Code:<?php
class Demo {
public $_name;
public function sayhello()
{
print "Hello {$this->getname()}!";
}
public function getname()
{
return $this->_name;
}
public function setname($name)
{
if(!is_string($name) || strlen($name) == 0) {
throw new Exception("Invalid name value");
}
this->_name = $name;
}
}
?>
----------------------------------------------
Oh - you are using PHP4! Well, if you are, use my code above and remove all keywords "public" and replace:
PHP Code:public $_name;
PHP Code:var $_name;
PHP Code:throw new Exception("Invalid name value");
PHP Code:echo "Invalid name value!";
-
Dec 6, 2004, 07:44 #12
Now i am using php 5.0.2.and the error reducing to
Parse error: syntax error, unexpected T_CLASS in d:\Inetpub\wwwroot\class.Demo.php on line 2
where corresponding my class.demo.php file is
<? php
class Demo {
private $_name;
public function sayhello() {
print "Hello ($this->getname()}!";
}
public function getname() {
return $this->_name;
}
public function setname($name) {
if(!is_string($name) || strlen($name) == 0 ) {
echo "Invalid name value!";
}
$this->_name = $name;
}
}
?>Plz solve it.I am bitting up.
-
Dec 6, 2004, 08:52 #13
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If I might make a suggestion, you need to read something like Chapter 2 in SitePoint's PHP Anthology 1, where it discusses the basics of object oriented programming.
You've violated all kinds of rules for OOP in your code. For example, you're not really returning anything of value, just the original name you passed to your object in the first place. You should also separate presentation from business rules.
Here is a cleaned up version of your code, which is what I think you're really trying to do:
Code:<? php class Demo { private $_name; public function setname($name) { if(!is_string($name) || strlen($name) == 0 ) { $return_message = "Invalid name value!"; } $this->_name = $name; } public function sayhello() { $return_message = "Hello".$this->getname."!"; $this->getname(); } public function getname() { return $return_message; } } ?>
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 6, 2004, 09:36 #14
- Join Date
- Oct 2003
- Location
- The Netherlands
- Posts
- 406
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think getname() should only echo the person's name and not something like "Hello <name>", because it would then get more than just the name. However, this may be only how I see it
-
Dec 6, 2004, 09:44 #15
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Kilroy
What I posted was only a suggestion anyway (as well as cleaning up the coding errors). I think what this person is really trying to do is just get a feel for how OOP works for PHP.Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 6, 2004, 10:04 #16
- Join Date
- May 2004
- Location
- North Pole
- Posts
- 450
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
heres yalls problem, you have all gone wayyy past the error.
Line 2,
look at line two. Hes told PHP its pubic, but forgot to desfine the variable.
Also, on OO, your using an object, so the functions in theroy only apply to that object, so going furthut, why would you call just $hello, if you have more than one clas open, your stuck. you would call $class->hello(), os i have also removed the public functions.
Have you tried
NB public var $var;
PHP Code:<? php
class Demo {
public var $_name;
function sayhello() {
print "Hello ($this->getname()}!";
}
function getname() {
return $this->_name;
}
function setname($name) {
if(!is_string($name) || strlen($name) == 0 ) {
echo "Invalid name value!";
}
$this->_name = $name;
}
}
?>
-
Dec 6, 2004, 10:30 #17
- Join Date
- Jun 2004
- Location
- South Africa
- Posts
- 28
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
class.Demo.php :
PHP Code:<?php
class Demo {
private $_name;
public function sayHello() {
echo 'Hello ('.$this->getname().'}!"';
}
public function getName() {
return $this->_name;
}
public function setName($name='') {
if(!is_string($name) || strlen($name) == 0) {
echo "Invalid name value!";
}
else {
$this->_name = $name;
}
}
}
?>
PHP Code:<?php
require_once('class.Demo.php');
$dem = new Demo();
$dem->setName('j996');
$dem->sayHello();
$dem->setName();
?>
-
Dec 6, 2004, 11:11 #18
- Join Date
- Oct 2003
- Location
- The Netherlands
- Posts
- 406
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Glad you worked it out!
vinyl-junkie, I understand that person just wants to learn OOPI was merely suggesting that a more appropriate name could be chosen for the method.
-
Dec 7, 2004, 03:05 #19
- Join Date
- May 2004
- Location
- North Pole
- Posts
- 450
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
i still dont see the point of having public function?
If you create a class then you can call it from where ever its been defined using $class->function(args)!!!
So why, just why>?
-
Dec 7, 2004, 08:36 #20
- Join Date
- May 2004
- Location
- Germany
- Posts
- 550
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
in php5 there are also private/protected functions/vars that can only be accessed inside the class (private) or a class extending it (protected)
with these two keywords public makes sense, without them not
-
Dec 7, 2004, 09:27 #21
- Join Date
- May 2004
- Location
- North Pole
- Posts
- 450
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
ok, i see your point, but why make it public?
Do you mean public as in you can call $class->publicfunction(), as appose to an error if you tryed $class->privatefunction();
Originally Posted by Daimaju
-
Dec 7, 2004, 13:56 #22
- Join Date
- Oct 2003
- Location
- The Netherlands
- Posts
- 406
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Infizi, that is indeed the reason. Private methods are, so to say, functions that can only be called by other methods in the particular class
Bookmarks