TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-23-2008, 05:19 AM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default uhh... OOP?

So here is what I *THINK* I gleaned from OOP...

To declare a new object, you use the new function like so:

PHP Code:
$object = new object(); 
I am not even sure if an object is an array. It is in Javascript :/

Then, a method is... a needlessly complex word for a function, in which every variable has to start with "this->", because this is used by the object to reference to itself...

A class, I remember, is something like the objects framework. It is like a blueprint for the object. The problem is, I don't know how.

Properties are another thing... I does not remember them.

Go easy on me... I am really confused right now.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-23-2008, 06:23 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

An Object in PHP is $this->object
$this grabs the property reference for that method.
And a method I think only referes to functions inside a class..
And such methods as Magic methods are called such as:
PHP Code:
function __construct();
function 
__destruct();
function 
__get();
function 
__set();
function 
__autoload();
function 
__isset();
function 
__unset(); 
And are only acceptable in PHP5, thus I love it!

More information on Magic Methods, Classes, Objects:
PHP: Classes and Objects (PHP 5) - Manual

Properties are things as those variables inside the class:
PHP Code:
class test
{
 public 
$test;
 public 
$foo;
 public 
$orc;

PHP4 had Var though it's still capable, it's more reasonably acceptable to use visibility: Public, Private, Protected

I usually use __construct and then add upon inside my functions, though you can only set your visibility on __construct, cause in my theory, it's making every single thing in that construct as the visibility:
PHP Code:
 public function __construct()
 {
   function 
yourfunction()
   {
     
// add whatever
   
}
 } 
[/php]


PHP Code:
$object = new object
This means you're assigning $object to get a new instance of object class, thus theres no parentheses.

Also, an object can infact be an array.


As said, above, to the link about PHP5 classes, objects, I'd study that and continue on with whatever you're doing! :]

I could go on forever, but just check the link, considering I don't want to make duplicate posts. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
The Following 2 Users Say Thank You to Orc For This Useful Post:
Aaron (01-23-2008), obolus (01-23-2008)
Old 01-23-2008, 06:38 AM   #3 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thanks orc, that helped a lot.

By the way I like the new signature.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-23-2008, 06:41 AM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
Thanks orc, that helped a lot.

By the way I like the new signature.
Thanks! And no problem, glad to help out anyone with what knowledge I have of the Language, :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 09:05 AM   #5 (permalink)
The Acquainted
 
obolus's Avatar
 
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
obolus is on a distinguished road
Default

Good stuff Orc.

VI wrote a nice OOP article a while back: Tutorial: PHP and OOP, a beginners guide
obolus is offline  
Reply With Quote
The Following 2 Users Say Thank You to obolus For This Useful Post:
Aaron (01-23-2008), Orc (01-23-2008)
Old 01-23-2008, 09:53 AM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

$this can only be used within the class though, and never outside.
Also, there are static variables and functions, and to refer to them you must use the self:: method.

PHP Code:
class staticClass {
public static 
$szVariable;

public static function 
setVar($szVar) {
self::$szVariable $szVar;
}


The good thing about this is that you don't have to create the object.

PHP Code:
include('staticClass.php');

staticClass::setVar('Hi there!');
echo 
staticClass::$szVariable
Output:
Code:
Hi there!
I use this method in my DB factory, and it's awesome!
Tanax is offline  
Reply With Quote
Old 01-23-2008, 09:58 AM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
$this can only be used within the class though, and never outside.
Also, there are static variables and functions, and to refer to them you must use the self:: method.

PHP Code:
class staticClass {
public static 
$szVariable;

public static function 
setVar($szVar) {
self::$szVariable $szVar;
}


The good thing about this is that you don't have to create the object.

PHP Code:
include('staticClass.php');

staticClass::setVar('Hi there!');
echo 
staticClass::$szVariable
Output:
Code:
Hi there!
I use this method in my DB factory, and it's awesome!
Isn't the alternative way of doing that would be
PHP Code:
staticClass->setVar('Hi There!');
staticClass->$szVariable
Considering I know thats the alternative way. or :s;S;d
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:07 AM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Isn't the alternative way of doing that would be
PHP Code:
staticClass->setVar('Hi There!');
staticClass->$szVariable
Considering I know thats the alternative way. or :s;S;d
No, the alternate way would be to instead of having the function and variable static, you just use the normals:

PHP Code:
class staticClass {
public 
$szVariable;

public function 
setVar($szVar) {
$this->szVariable $szVar;
}


Also note that you have to use $this instead of self:: when referring to $szVariable inside the class.

But then you have to create the object:

PHP Code:
include('staticClass.php');

$object = new staticClass();
$object->setVar('Hi there!');
echo 
$object->szVariable

In this simple example, static method doesn't seem very useful, but I promise that it's a GREAT method when using a DB factory.

If this topic is still going when I get home from school in about 3 hours, I'll post my DB factory, and explain how to use it..
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
Orc (01-23-2008)
Old 01-23-2008, 10:10 AM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
No, the alternate way would be to instead of having the function and variable static, you just use the normals:

PHP Code:
class staticClass {
public 
$szVariable;

public function 
setVar($szVar) {
$this->szVariable $szVar;
}


Also note that you have to use $this instead of self:: when referring to $szVariable inside the class.

But then you have to create the object:

PHP Code:
include('staticClass.php');

$object = new staticClass();
$object->setVar('Hi there!');
echo 
$object->szVariable

In this simple example, this method doesn't seem very useful, but I promise that it's a GREAT method when using a DB factory.

If this topic is still going when I get home from school in about 3 hours, I'll post my DB factory, and explain how to use it..
Thanks for clearly that up for me, I've only been doing PHP for about 8 months to a year now so..

Off Topic:
GTalk notification of a new Email on the GMail email system has a long delay, lol.

By the way, you could just use the __autoload() magic method for grabbing php classes, :) though it doesnt work in the CLI
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:20 AM   #10 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Thanks for clearly that up for me, I've only been doing PHP for about 8 months to a year now so..

Off Topic:
GTalk notification of a new Email on the GMail email system has a long delay, lol.

By the way, you could just use the __autoload() magic method for grabbing php classes, :) though it doesnt work in the CLI
Wow, really?
You're learning fast

Here's a more advanced example, taken from another thread:
PHP Code:
class DBfactory
{
    public static 
$pDB;

    public static function &
factory($szType "")
    {
        if(!
is_object(self::$pDB))
        {
            switch(
$szType)
            {
                case 
'mysql':
                    
self::$pDB = new DBmysql;
                    break;
                case 
'mssql':
                    
self::$pDB = new DBmssql;
                    break;
                default:
                    
self::$pDB = new DBmysql;
                    break;
            }
        }
        return 
self::$pDB;
    }

And to use it:
PHP Code:
$db DBfactory::factory("mysql");
$db->query(".."); 
If the DBfactory wouldn't have static function and variables, you would have to declare the object first:
PHP Code:
$DBfactory = new DBfactory();
$db $DBfactory('mysql'); 
And then we can use the functions of the db object such as for example executing a query.

So the advantage here is that you don't have to create the object DBfactory since it's static, and in the actual static function, it create the object.. and returns it.

Ofcourse you have to include the files containing the classes of the objects you want to create. ANd this can be used not just for DB. I'm using it as a ALLround system.

PHP Code:
$db DB::getInstance('DBmysql');
TANAXIA::setDB($db);
$news TANAXIA::getInstance('news');
$forum TANAXIA::getInstance('forum'); 
And so on..
I have 2 factory classes there as you can see.
One for the DB, and it contains the creation of objects of
DBmysql
DBmysqli
DBoracle

etcetc..

And TANAXIA contains the creation of objects for misc things using the DB class.


Using the factory pattern (mad rantings of a mind without coffee)
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
Orc (01-23-2008)
Old 01-23-2008, 10:28 AM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Wow, really?
You're learning fast

Here's a more advanced example, taken from another thread:
PHP Code:
class DBfactory
{
    public static 
$pDB;

    public static function &
factory($szType "")
    {
        if(!
is_object(self::$pDB))
        {
            switch(
$szType)
            {
                case 
'mysql':
                    
self::$pDB = new DBmysql;
                    break;
                case 
'mssql':
                    
self::$pDB = new DBmssql;
                    break;
                default:
                    
self::$pDB = new DBmysql;
                    break;
            }
        }
        return 
self::$pDB;
    }

And to use it:
PHP Code:
$db DBfactory::factory("mysql");
$db->query(".."); 
If the DBfactory wouldn't have static function and variables, you would have to declare the object first:
PHP Code:
$DBfactory = new DBfactory();
$db $DBfactory('mysql'); 
And then we can use the functions of the db object such as for example executing a query.

So the advantage here is that you don't have to create the object DBfactory since it's static, and in the actual static function, it create the object.. and returns it.

Ofcourse you have to include the files containing the classes of the objects you want to create. ANd this can be used not just for DB. I'm using it as a ALLround system.

PHP Code:
$db DB::getInstance('DBmysql');
TANAXIA::setDB($db);
$news TANAXIA::getInstance('news');
$forum TANAXIA::getInstance('forum'); 
And so on..
I have 2 factory classes there as you can see.
One for the DB, and it contains the creation of objects of
DBmysql
DBmysqli
DBoracle

etcetc..

And TANAXIA contains the creation of objects for misc things using the DB class.


Using the factory pattern (mad rantings of a mind without coffee)
Thanks! Since I'm basically asleep here, it's kinda hard for me to concentrate, the reason I'm up is cause code doesn't bore me like other people :P but umm, it's 5:26 AM >.<

As stated in my Profile, I'm addicted to and I'm in love with Computers, true nerd here xD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:30 AM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
Thanks! Since I'm basically asleep here, it's kinda hard for me to concentrate, the reason I'm up is cause code doesn't bore me like other people :P but umm, it's 5:26 AM >.<

As stated in my Profile, I'm addicted to and I'm in love with Computers, true nerd here xD
Haha :D
It's 11.30AM here :O

;) You live in canada or something? USA.. :P
Tanax is offline  
Reply With Quote
Old 01-23-2008, 10:33 AM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Haha :D
It's 11.30AM here :O

;) You live in canada or something? USA.. :P
Canada

but umm, I'm trying about 4 languages at once so I try them at once, some how I manage to keep up with them. I'm one of those people who have a million things happening their brains, their brains go faster than the speed of Light. :D
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:39 AM   #14 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

By the way, I got PHP Architecs, Zend Certification Study guide, it's awesome as to which contains a lot about OOP(classes and objects), More about Arrays, XML, Database Programming, and more..

By the way, expect me to be on TalkPHP for the period of time it's up, thus that'll probably be never, so expect me to be here in 20 years, LOL.. XD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:40 AM   #15 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

When did this thread get hijacked...?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 01-23-2008, 10:42 AM   #16 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
When did this thread get hijacked...?
When Tanax arrived. :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 10:45 AM   #17 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Well, I'm starting to daze off, I'm going to jet! bY Everyone
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 01-23-2008, 12:50 PM   #18 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

to go back to the original post:
Quote:
Then, a method is... a needlessly complex word for a function,
it isnt when you think about it, a method is a method or an action of an object, although you could still say function and it would make sense, using method helps clarify the difference between normal user defined functions and class methods, thats just my own personal opinion
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-25-2008, 06:10 PM   #19 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Whoa Orc 's on a roll here. I'm not a hero on OOP myself but I find it very useful when talking about sessions, mysql and for example, form filtering. Same goes for making your own imageclass.

Is there a way to make it compatible with PHP 4 as well? Because I recon not ALL host have updates to PHP 5 yet.

/edit
Even my cheap ass host is still using
PHP Version 4.4.8
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 01-25-2008, 06:42 PM   #20 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
Whoa Orc 's on a roll here. I'm not a hero on OOP myself but I find it very useful when talking about sessions, mysql and for example, form filtering. Same goes for making your own imageclass.

Is there a way to make it compatible with PHP 4 as well? Because I recon not ALL host have updates to PHP 5 yet.

/edit
Even my cheap ass host is still using
PHP Version 4.4.8
PHP4's OOP capacity is pathetic, for the most part you cant make it compatible.
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design