[PHP] Public in Classes

2008-09-21 Thread Ben Stones
Hi,

Just started with object oriented programming and glad I have come across
it, just need a few things clearing up.

When I make a variable or method public, does this mean that it can be used
outside of classes, for instance in other classes or as well as
instantiating classes? So if I made it private, it wouldn't be able to be
instantiated or extended in other classes, am I right?

I have never added public when I am creating methods so I presume its
already set as default if you don't add it?

Hope you can understand my question.

Cheers.


Re: [PHP] Public in Classes

2008-09-21 Thread Ólafur Waage
Here you go.

http://talks.php.net/show/php5_ca/4

Ólafur Waage
[EMAIL PROTECTED]

2008/9/21 Ben Stones [EMAIL PROTECTED]:
 Hi,

 Just started with object oriented programming and glad I have come across
 it, just need a few things clearing up.

 When I make a variable or method public, does this mean that it can be used
 outside of classes, for instance in other classes or as well as
 instantiating classes? So if I made it private, it wouldn't be able to be
 instantiated or extended in other classes, am I right?

 I have never added public when I am creating methods so I presume its
 already set as default if you don't add it?

 Hope you can understand my question.

 Cheers.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Public in Classes

2008-09-21 Thread Jochem Maas

Ben Stones schreef:

Hi,

Just started with object oriented programming and glad I have come across
it, just need a few things clearing up.

When I make a variable or method public, does this mean that it can be used
outside of classes, for instance in other classes or as well as
instantiating classes? So if I made it private, it wouldn't be able to be
instantiated or extended in other classes, am I right?

I have never added public when I am creating methods so I presume its
already set as default if you don't add it?

Hope you can understand my question.


yes, and the answer nearly always lies in trying it out, run this
(and if/when you hit a fatal error, comment the offending line and run it 
again):

?php

class Test
{
public  $a = A;
protected   $b = B;
private $c = C;

function tryme()
{
echo $this-a, \n;
echo $this-b, \n;
echo $this-c, \n;
echo $this-d, \n;
}   
}

class TestTwo
{
function tryme()
{
echo $this-a, \n;
echo $this-b, \n;
echo $this-c, \n;
echo $this-d, \n;
}
}

$t1 = new Test;
$t2 = new TestTwo;

$t1-tryme();

echo $t1-a, \n;
echo $t1-b, \n;
echo $t1-c, \n;
echo $t1-d, \n;

$t2-tryme();

echo $t2-a, \n;
echo $t2-b, \n;
echo $t2-c, \n;
echo $t2-d, \n;

?


Cheers.




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Public in Classes

2008-09-21 Thread Ben Stones
Hi,

I have this piece of code that I have created:

class userQueries {
public function numberUsers() {
$get_users=mysql_query(SELECT * FROM `users` WHERE `online` 
NOW()) or exit(../includes/error.php);
}
}

class usersOnline extends userQueries {
public function usersOnline() {
echo mysql_num_rows($this-numberUsers);
}
}

How do I request specific methods in other classes? The error that comes up
is:

mysql_num_rows(): supplied argument is not a valid MySQL result resource

which is somewhat expected as $this only refers to the variables and there
are no variables called numberUsers.

Cheers!

2008/9/21 Jochem Maas [EMAIL PROTECTED]

 Ben Stones schreef:

  Hi,

 Just started with object oriented programming and glad I have come across
 it, just need a few things clearing up.

 When I make a variable or method public, does this mean that it can be
 used
 outside of classes, for instance in other classes or as well as
 instantiating classes? So if I made it private, it wouldn't be able to be
 instantiated or extended in other classes, am I right?

 I have never added public when I am creating methods so I presume its
 already set as default if you don't add it?

 Hope you can understand my question.


 yes, and the answer nearly always lies in trying it out, run this
 (and if/when you hit a fatal error, comment the offending line and run it
 again):

 ?php

 class Test
 {
public  $a = A;
protected   $b = B;
private $c = C;

function tryme()
{
echo $this-a, \n;
echo $this-b, \n;
echo $this-c, \n;
echo $this-d, \n;
}
 }

 class TestTwo
 {
function tryme()
{
echo $this-a, \n;
echo $this-b, \n;
echo $this-c, \n;
echo $this-d, \n;
}
 }

 $t1 = new Test;
 $t2 = new TestTwo;

 $t1-tryme();

 echo $t1-a, \n;
 echo $t1-b, \n;
 echo $t1-c, \n;
 echo $t1-d, \n;

 $t2-tryme();

 echo $t2-a, \n;
 echo $t2-b, \n;
 echo $t2-c, \n;
 echo $t2-d, \n;

 ?

  Cheers.





Re: [PHP] Public in Classes

2008-09-21 Thread Ashley Sheridan
On Sun, 2008-09-21 at 12:24 +0100, Ben Stones wrote:
 Hi,
 
 Just started with object oriented programming and glad I have come across
 it, just need a few things clearing up.
 
 When I make a variable or method public, does this mean that it can be used
 outside of classes, for instance in other classes or as well as
 instantiating classes? So if I made it private, it wouldn't be able to be
 instantiated or extended in other classes, am I right?
 
 I have never added public when I am creating methods so I presume its
 already set as default if you don't add it?
 
 Hope you can understand my question.
 
 Cheers.
As far as I understand it, public methods of a class let you use it on
instances of that class, but private methods can only be called from
within the class itself, if that makes any sense?


Ash
www.ashleysheridan.co.uk


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Public in Classes

2008-09-21 Thread Jochem Maas

Ben Stones schreef:

Hi,

I have this piece of code that I have created:

class userQueries {
public function numberUsers() {
$get_users=mysql_query(SELECT * FROM `users` WHERE `online` 
NOW()) or exit(../includes/error.php);
}
}

class usersOnline extends userQueries {
public function usersOnline() {
echo mysql_num_rows($this-numberUsers);
}
}

How do I request specific methods in other classes? The error that comes up
is:

mysql_num_rows(): supplied argument is not a valid MySQL result resource

which is somewhat expected as $this only refers to the variables and there
are no variables called numberUsers.

Cheers!


1. don't double post, a bit of patience is in order.
2. this has nothing to do with public/private methods.
3. use braces to call a method ... methods are not variables.
4. you can't pass an include file to exit and expect it to run that file
(it will just echo out the string you gave it)
5. the 'foo() or exit()' error handling strategy is rather crap (imho)
... you could do it gracefully instead of just killing the script.
6. I doubt whether `online`  NOW() will give you the result your looking for

class userQueries {
public function numberUsers($where = 1) {
$r = mysql_query(SELECT * FROM `users` WHERE $where) or 
$this-killMe();
$n = mysql_num_rows($r);
mysql_free_result($r);

return $n;
}

protected function killMe()
{
include ../includes/error.php;
exit(1);
}
}

class usersOnline extends userQueries {
public function usersOnline() {
return $this-numberUsers(`online`  NOW());
}
}



2008/9/21 Jochem Maas [EMAIL PROTECTED]


Ben Stones schreef:

 Hi,

Just started with object oriented programming and glad I have come across
it, just need a few things clearing up.

When I make a variable or method public, does this mean that it can be
used
outside of classes, for instance in other classes or as well as
instantiating classes? So if I made it private, it wouldn't be able to be
instantiated or extended in other classes, am I right?

I have never added public when I am creating methods so I presume its
already set as default if you don't add it?

Hope you can understand my question.


yes, and the answer nearly always lies in trying it out, run this
(and if/when you hit a fatal error, comment the offending line and run it
again):

?php

class Test
{
   public  $a = A;
   protected   $b = B;
   private $c = C;

   function tryme()
   {
   echo $this-a, \n;
   echo $this-b, \n;
   echo $this-c, \n;
   echo $this-d, \n;
   }
}

class TestTwo
{
   function tryme()
   {
   echo $this-a, \n;
   echo $this-b, \n;
   echo $this-c, \n;
   echo $this-d, \n;
   }
}

$t1 = new Test;
$t2 = new TestTwo;

$t1-tryme();

echo $t1-a, \n;
echo $t1-b, \n;
echo $t1-c, \n;
echo $t1-d, \n;

$t2-tryme();

echo $t2-a, \n;
echo $t2-b, \n;
echo $t2-c, \n;
echo $t2-d, \n;

?

 Cheers.







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php