 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
01-23-2008, 05:19 AM
|
#1 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
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.
|
|
|
01-23-2008, 06:23 AM
|
#2 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
|
The Following 2 Users Say Thank You to Orc For This Useful Post:
|
|
01-23-2008, 06:38 AM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
Thanks orc, that helped a lot.
By the way I like the new signature.
|
|
|
01-23-2008, 06:41 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Aaron
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
|
|
|
|
01-23-2008, 09:05 AM
|
#5 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Location: florida
Posts: 110
Thanks: 36
|
Good stuff Orc.
VI wrote a nice OOP article a while back: Tutorial: PHP and OOP, a beginners guide
|
|
|
|
|
The Following 2 Users Say Thank You to obolus For This Useful Post:
|
|
01-23-2008, 09:53 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
$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:
I use this method in my DB factory, and it's awesome!
|
|
|
|
01-23-2008, 09:58 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
$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:
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
|
|
|
|
01-23-2008, 10:07 AM
|
#8 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Orc
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..
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
01-23-2008, 10:10 AM
|
#9 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Tanax
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
|
|
|
|
01-23-2008, 10:20 AM
|
#10 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Orc
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)
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
01-23-2008, 10:39 AM
|
#11 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
01-23-2008, 10:40 AM
|
#12 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
When did this thread get hijacked...?
|
|
|
01-23-2008, 10:42 AM
|
#13 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Aaron
When did this thread get hijacked...?
|
When Tanax arrived. :P
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
01-23-2008, 10:45 AM
|
#14 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Well, I'm starting to daze off, I'm going to jet! bY Everyone
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
01-23-2008, 12:50 PM
|
#15 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
01-25-2008, 06:10 PM
|
#16 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
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"
|
|
|
01-25-2008, 06:42 PM
|
#17 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by ReSpawN
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.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|