Jump to content


oop :: method _construct()


9 replies to this topic

#1 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 09 July 2006 - 11:29 AM

hai, now i learn PHP but with OOP style. And i have a problem :
source code :

<?
class Person
{
private $name;

function _construct($name)
{
$this->name=$name;
}

function getName()
{
return $this->name;
}
}

$judy = new Person("Judy") . "<br>";
$joe = new Person("Joe") ."<br>";

echo $judy->getName();
echo $joe->getName();
?>

error :
Fatal error: Call to a member function getName() on a non-object in
... on line 18

i use php version 5.1.0.

Please, correct me. :) :D

Thanx a lot... :D :D

#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 09 July 2006 - 11:39 AM

well first off what error are you getting?

#3 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 09 July 2006 - 11:44 AM

Read deadly, lol.

Fatal error: Call to a member function getName() on a non-object in
... on line 18

Use it like this to work:

<?
class Person
{
	private $name;

	function _construct($name)
	{
		$this->name = $name;
	}

	function getName()
	{
		return $this -> name;
	}
}

$cl = new Person();

$cl -> _construct("Judy");

echo $cl -> getName();
?>

btw. Doing...

$cl -> _construct("Name");

...again will not add a new name, it will simply overwrite the current variable value.

Edited by .Matt, 09 July 2006 - 11:48 AM.


#4 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 09 July 2006 - 11:55 AM

matt, i wanna ask u. Better i use OOP style for coding or use general style ( not use class and new....)

thanx... :)

#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 09 July 2006 - 12:07 PM

OOP and functions etc kind of "constrain" coding ,makes it faster,neater and a lot less stressful on the coder lol.

I say use class based as the advantages are great. E.g. using a mysql class allows you to have constant error handling etc etc.

There are very few reasons why not to, thats what it comes down to really.

Edited by .Matt, 09 July 2006 - 12:09 PM.


#6 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 09 July 2006 - 12:22 PM

View Post.Matt, on Jul 10 2006, 12:07 AM, said:

OOP and functions etc kind of "constrain" coding ,makes it faster,neater and a lot less stressful on the coder lol.

I say use class based as the advantages are great. E.g. using a mysql class allows you to have constant error handling etc etc.

There are very few reasons why not to, thats what it comes down to really.

yeah, we must learn alot of Java to know OOP style. :) :lol:
But, i agree with u.
Nice code, Thanx. :) :D

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 July 2006 - 12:23 PM

Three quick things i want to point out.

1. The reason for the error is that you set $judy and $joe as strings (since you added ."<br>" after declaring the object).

2. Matt is right in having to call the _construct function, but you could simplify things if you added another underscore before it.
http://php.net/manua....oop5.decon.php
class Person{
	private $name;

	function __construct($name){
		$this->name = $name;
	}

	function getName(){
		return $this -> name;
	}
}

$judy = new Person("Judy");
$joe = new Person("Joe");

echo $judy->getName()."<br>";
echo $joe->getName();
3. Calling the construct function of the same object will simply overwrite the name on that object. But, making a new object will make a new object, no harm, no foul. But, it would be wise to give your variables more general names (like how matt called the variable $cl, not $judy), that way, when you go to change something, you don't have to change every variable in order to still follow it.

#8 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 09 July 2006 - 12:36 PM

OK. i've clear about that. thanx RC69, Matt, and Deadly.

i've just learn OOP style... :) :lol:

#9 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 10 July 2006 - 03:22 AM

lets just be honest I didn't help, i just didn't read.

#10 joe

    Young Padawan

  • Members
  • Pip
  • 115 posts
  • Location:stuck in the middle of cyber space

Posted 10 July 2006 - 10:01 PM

View Postdeadly, on Jul 10 2006, 03:22 PM, said:

lets just be honest I didn't help, i just didn't read.

:D :P it's ok !!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users