...

View Full Version : PHP Syntax Similarities to Actionscript ---



JimNayzium
07-24-2008, 08:53 PM
I have a good working knowledge of Flash Actionscript 2.0 and its syntax.

I am hoping to become more advanced with Javascript and PHP.

The buzz-word seems to be OOP.

I have a quick question...

I completely understand in Actionscript a "type" or "object" having "methods."

Meaning you can say

MovieClip.alpha = 50;

Because the MovieClip Object has a method called alpha...

I use these methods all the time.

Is OOP in essence just that?

Somewhere in the bowels of the flash program code is a simple class or function or OBJECT named MovieClip....

and inside it are a list of functions...one of which is

function alpha() {
}

-----

If that line of reasoning is hypothetically true, and therefore OOP demonstrated in my imagination....

Here is my question about PHP OOP...and syntax...

If in flash we use dot.syntax to access the methods...

is that what in PHP becomes =>

??

Some one please point me to the most basic of tutorials on the actual syntax of the object-oriented-programming in PHP 5 specifically....

I am always hacking someone elses open-source stuff and I always get lost with the Colons

like -- someTextHere::SomeOtherThingHERE::

and with the equals arrows...

someThingHere=>someOtherthingHere

THANKS!

Fou-Lu
07-24-2008, 11:42 PM
Thats exactly how OOP is. In PHP, its identified using '->' ($obj->myMethod()); => is used for separation of array key/value pairs and in foreach syntax. :: is used as a static method identifier, but in PHP 6 namespaces will be used, so we can identify which we are looking for via the namespace. Static values are useful for direct association with a class, but not as an object. Consider having a user object that tracks a userid and username. A static method would be something like getusers() which returns an array or collection of all the users in the database. The biggest thing to remember with static methods is that they cannot make use of '$this', which is reserved for an object context.

I think that gets all of the questions you asked.

JimNayzium
07-27-2008, 06:11 PM
What a great answer!! THANKS!

can you clarify the differences (by using simple getuser() type examples...)

on exactly what the :: is for ?

I get

$oject->myMethod() -- that makes good sense to me.

I understand the => is for arrays....

but still don't quite get the ::

Also -- as a favor -- don't reference the php 6 stuff yet as I am just learning and it confusese me....(not sure what namespaces are even.)

Fou-Lu
07-27-2008, 08:43 PM
Sure, we'll make a simple object for this. We'll use the user one mentioned above.


<?php

class User
{
private $userID;
private $userName;

public function __construct($userID = 0, $userName = '')
{
$this->userID = $userID;
$this->userName = $userName;
}
public function getUserName()
{
return $this->userName;
}
public static function getAllUsers()
{
$result = array();
// Qry the db or open file or whatever. $obRes will contain this resource
while ($row = $obRes->fetchArray())
{
$result[] = new User((int)$row['userID'], $row['userName']);
}
return $result;
}
}
?>

Now, the concept behind the user object is to store a single user. When we want to get all users, we use a static method embedded within the user class using the Paamayim Nekudotayim operator ( :: ). For simplicity, we call it scope resolution.


$user = new User(1, 'Fou-Lu'); // Create a new user with the name Fou-Lu
$allUsers = User::getAllUsers(); // Returns a User[] with all users.


There are two exceptions to the use of Paamayim Nekudotayim, parent and self. These refer to extended child objects and allow you to call specific methods identified either from the parent (eg: parent::getUsername()) or child (self::getUsername()). The getUsername is a bad example though since it would be a non-static method. Polymorphism would dictate that the child's getUsername method would override the parent's getUsername method, so a simple $this->getUsername would work from the object context.

OO methodology takes a while to master. Just keep practicing at it. I'd probably recommend looking into a language like Java - the syntax is even more like AS than PHP is, and java is a true OO language unlike PHP.

Hope that helps more than that confuses ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum