Jump to content


PHP functions


5 replies to this topic

#1 gold killer

    Young Padawan

  • Members
  • Pip
  • 14 posts

Posted 07 February 2007 - 06:59 AM

Its more of a question really.
Ive recently seen people using functions like so:

public function A()
{
 //function here
}


private function B()
{
 //function here
}

rather than just

function C()
{
 //function here
}

So what does putting private or public infront of function do?

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 07 February 2007 - 12:52 PM

PHP 5 Class Visibility

Basically those keywords just simply tell the overall class what can call that function, and what can modify it. For example, public is well, public, as it can be called from anywhere in or outside the class, can be over-ridden by another class that extends the class, and the such, whereas a private class cannot be called from outside the class, and can only be called from inside the class itself.

#3 gold killer

    Young Padawan

  • Members
  • Pip
  • 14 posts

Posted 07 February 2007 - 05:03 PM

ok, thanks for your help

#4 upstream

    Young Padawan

  • Members
  • Pip
  • 8 posts
  • Location:Laguna, Philippines

Posted 08 February 2007 - 03:25 AM

oh..i dunno you can use that kind of function declaration in php...so the default class visibility is private?

#5 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 08 February 2007 - 12:24 PM

No, the default is public. :D

#6 cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 08 February 2007 - 02:55 PM

class myClass
{
  public function A()
  {
	echo "Hello!";
  }
  private function B()
  {
	echo "Hi!";
  }
  public function C()
  {
	$this->B();
  }
}
$class = new myClass;
$class->A(); // Echoes "Hello!"
$class->B(); // Gives an error
$class->C(); // Echoes "Hi!"






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users