stdClass - A newbie question

stdClass - A newbie question

am 30.07.2009 12:21:09 von talofo talofo

Hello everybody,=20


In this class sketch:



class Pagination {

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


{

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

$page =3D max($page, 1);

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

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


=20
$pagi_obj=3D new stdClass;

$pagi_obj->total_pages =3D $total_pages;

$pagi_obj->offset =3D $offset;

$pagi_obj->limit =3D $limit;

$pagi_obj->page;



return $pagi_obj;

=20

} =20

=20
}


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?=20
Why do we keep the values passed as params on method Pagination inside =
this
stdClass object, and not inside Pagination own properties for example?=20

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

Regards,
M=E1rcio


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

AW: stdClass - A newbie question

am 30.07.2009 13:10:04 von 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
=20
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.=20



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

Hello everybody,=20


In this class sketch:



class Pagination {

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


{

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

$page =3D max($page, 1);

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

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


=20
$pagi_obj=3D new stdClass;

$pagi_obj->total_pages =3D $total_pages;

$pagi_obj->offset =3D $offset;

$pagi_obj->limit =3D $limit;

$pagi_obj->page;



return $pagi_obj;

=20

} =20

=20
}


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?=20
Why do we keep the values passed as params on method Pagination inside =
this
stdClass object, and not inside Pagination own properties for example?=20

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

Regards,
Márcio


--=20
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: stdClass - A newbie question

am 30.07.2009 13:38:16 von talofo talofo

> 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.h tml
=20
=20
> 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=20
{
Public static function Pagination ($limit, $total_records, $page)
{
$pagi_obj=3D new stdClass;

$pagi_obj->total_pages =3D $total_pages;

$pagi_obj->offset =3D $offset;

$pagi_obj->limit =3D $limit;

$pagi_obj->page =3D $page;



return $pagi_obj;
....

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

?

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

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





=20


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

Re: stdClass - A newbie question

am 30.07.2009 17:52:50 von 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.h tml
>
>
>> 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