[PHP] stdClass - A newbie question

2009-07-30 Thread MEM
Hello everybody, 


In this class sketch:


?php

class Pagination {

...
  
public static function Pagination ($total_records, $limit, $page)


{

  $total_pages = ceil($total_records / $limit);

$page = max($page, 1);

$page = min($page, $total_pages);

$offset = ($page -1) * $limit;



$pagi_obj= new stdClass;

$pagi_obj-total_pages = $total_pages;

$pagi_obj-offset = $offset;

$pagi_obj-limit = $limit;

$pagi_obj-page;



return $pagi_obj;



}  


}


Why do the author used a stdClass ?
What are the advantages of using a stdClass?

Since we are already inside a class, why do we need to create a new object
from another class, inside this one? 
Why do we keep the values passed as params on method Pagination inside this
stdClass object, and not inside Pagination own properties for example? 

Any help clarifying this, knowing that I'm a newbie,

Regards,
Márcio


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



AW: [PHP] stdClass - A newbie question

2009-07-30 Thread Bouz Alexander
Hello Márcio,

stdClass is simply an empty class, without any properties or methods.

The object in the code sample is used to return multiple values at once. He 
could also have used an associative array to achieve that, but by choosing an 
object, he shows his affinity to the object oriented programming style.

The empty stdClass has the advantage of being a blank class. You can assign 
new properties or methods without minding about properties and methods that 
already exist in the class.
Besides that, it also doesn't use much memory. (Don't see that as performance 
boosting tip! There are much better ways to do that. Besides that, 
comprehensible design  performance.)

By the way, there are many reasons for creating objects inside of other 
objects. This should not be considered an exception. I don't know where this 
code belongs to, so I can't clear out if it is good or bad OOP style.

Just think about everything in classes and objects - even return values or 
other things, that you would normally consider as volatile. (eg. a network 
connection)

Have a nice day,
Alex





Austrian Optic Technologies GmbH
Eisgrubengasse 2-6, A-2334 Vösendorf/Austria
Firmenbuch Nr.: FN 93629s
Firmenbuchgericht: Landesgericht Wien
UID-NR.: ATU 14976908
 
Disclaimer: Diese Nachricht ist ausschließlich für den bezeichneten Adressaten 
oder deren Vertreter bestimmt. Sollten sie nicht der vorgesehene Adressat 
dieser E-Mail sein, so bitten wir Sie, sich mit dem Absender der E-Mail in 
Verbindung zu setzen. Jede Form der unauthorisierten Nutzung, Veröffentlichung, 
Vervielfältigung oder Weitergabe dieser E-Mail ist nicht gestattet.
This message is exclusively intended for the designated recipient or his 
representatives. If you are not the designated recipient of this e-mail, we 
kindly ask you to notify the sender of this e-mail. Any form of unauthorized 
use, publication, duplication or dissemination of this e-mail is prohibited. 



Von: MEM [mailto:tal...@gmail.com] 
Gesendet: Donnerstag, 30. Juli 2009 12:21
An: php-general@lists.php.net
Betreff: [PHP] stdClass - A newbie question

Hello everybody, 


In this class sketch:


?php

class Pagination {

...
  
public static function Pagination ($total_records, $limit, $page)


{

  $total_pages = ceil($total_records / $limit);

$page = max($page, 1);

$page = min($page, $total_pages);

$offset = ($page -1) * $limit;



$pagi_obj= new stdClass;

$pagi_obj-total_pages = $total_pages;

$pagi_obj-offset = $offset;

$pagi_obj-limit = $limit;

$pagi_obj-page;



return $pagi_obj;



}  


}


Why do the author used a stdClass ?
What are the advantages of using a stdClass?

Since we are already inside a class, why do we need to create a new object
from another class, inside this one? 
Why do we keep the values passed as params on method Pagination inside this
stdClass object, and not inside Pagination own properties for example? 

Any help clarifying this, knowing that I'm a newbie,

Regards,
Márcio


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

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



RE: [PHP] stdClass - A newbie question

2009-07-30 Thread MEM
 By the way, there are many reasons for creating objects inside of other
 objects. This should not be considered an exception. I don't know where
 this code belongs to, so I can't clear out if it is good or bad OOP
 style.

I do not intend to public judge the author, but the original article is here, 
just for proper credit:
http://www.phpro.org/tutorials/Pagination-with-PHP-and-PDO.html
 
 
 Just think about everything in classes and objects - even return values
 or other things, that you would normally consider as volatile. (eg. a
 network connection)

It would be a nice exercise to practice. :) Thanks for the tip.


And thanks a lot for the reply, I'm almost there... one last newbie question:

When we have something like this:
Class Pagination 
{
Public static function Pagination ($limit, $total_records, $page)
{
$pagi_obj= new stdClass;

$pagi_obj-total_pages = $total_pages;

$pagi_obj-offset = $offset;

$pagi_obj-limit = $limit;

$pagi_obj-page = $page;



return $pagi_obj;
...

How can we, later, have something like this, for example:
$pagination_obj=Pagination::Pagination(some params)
$pagination_obj-offset;

?

I mean:
When we instantiate the class by doing: 
$pagination_obj=Pagination::Pagination(some params) 

We will have an object ($pagi_obj) returned where the properties of that object 
will be *referring* to the values passed on the method argument, right?

How does those $pagi_obj properties, can then be accessible by doing 
$pagination_obj-offset; ? I mean, they are attributes of our stdClass object 
(aka pagi_obj), and they are not attributes of our Pagination class, or are 
they?


Thanks in advance,
Márcio





 


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



Re: [PHP] stdClass - A newbie question

2009-07-30 Thread Shawn McKenzie
MEM wrote:
 By the way, there are many reasons for creating objects inside of other
 objects. This should not be considered an exception. I don't know where
 this code belongs to, so I can't clear out if it is good or bad OOP
 style.
 
 I do not intend to public judge the author, but the original article is here, 
 just for proper credit:
 http://www.phpro.org/tutorials/Pagination-with-PHP-and-PDO.html
  
  
 Just think about everything in classes and objects - even return values
 or other things, that you would normally consider as volatile. (eg. a
 network connection)
 
 It would be a nice exercise to practice. :) Thanks for the tip.
 
 
 And thanks a lot for the reply, I'm almost there... one last newbie question:
 
 When we have something like this:
 Class Pagination 
 {
   Public static function Pagination ($limit, $total_records, $page)
   {
   $pagi_obj= new stdClass;
 
   $pagi_obj-total_pages = $total_pages;
 
   $pagi_obj-offset = $offset;
 
   $pagi_obj-limit = $limit;
 
   $pagi_obj-page = $page;
 
 
 
   return $pagi_obj;
 ...
 
 How can we, later, have something like this, for example:
 $pagination_obj=Pagination::Pagination(some params)
 $pagination_obj-offset;
 
 ?
 
 I mean:
 When we instantiate the class by doing: 
 $pagination_obj=Pagination::Pagination(some params) 

Here you are calling the static method of the Pagination class which
returns the stdClass object which you are assigning to $pagination_obj.

 
 We will have an object ($pagi_obj) returned where the properties of that 
 object will be *referring* to the values passed on the method argument, right?

$pagi_obj is what is was in the Pagination class, but when it was
returned you assigned it to $pagination_obj.


 How does those $pagi_obj properties, can then be accessible by doing 
 $pagination_obj-offset; ? I mean, they are attributes of our stdClass object 
 (aka pagi_obj), and they are not attributes of our Pagination class, or are 
 they?

The Pagination class built the object for you and assigned those vars to
it.  It then returned the object and you assigned it the name
$pagination_obj.

HTH

-- 
Thanks!
-Shawn
http://www.spidean.com

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