homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
starting with OOP in php
last77

5+ Year Member



 
Msg#: 3931134 posted 10:53 am on Jun 11, 2009 (gmt 0)

hi to all!

I'm a newbie about the OOP with php, and I'm triyng to learn this. Well have a problem that I hope someone can help me.

I've created 3 file :
config.php
class_queries.php
index.php

in the config I've create the costant and the class to connect to the db :

<?php
/*
* Connection class for database Mysql
* Define the parameter for the website
* and connect to the database using $connection variable.
*/
define('DB_HOST','localhost');
define('DB_NAME','domain');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('HOST','http://localhost:8888/');
define('DOMAIN','domain.com/');
define('URL',HOST.DOMAIN);
class class_connect {
//put your code here
function __construct(){

}
function connect(){
try{
$conn = mysql_pconnect(DB_HOST,DB_USER,DB_PASSWORD) or die("Errore di connessione");
mysql_select_db(DB_NAME);
echo "connection ok";
}
catch(error $e){
echo 'Error : '.$e->getMessage();
}
}
}
$connection = new class_connect();
$connection->connect();

?>

I've used

echo "connection ok";

to verify the connection, now i've included this file in the class_queries.php file :

<?php
include_once('config.php');
class first{

function __construct(){

}
function get_last_five(){
echo "the class works";
}
}
?>


And finally included the file in index.php.

My problem is this : if I create the object instance in the class_queries.php it works, but if I create the object in the index.php it doesn't works.

The path is correct, and I've used this code:

$obj = new first;
$obj->get_lsat_five();

I cant understand why doesn't work in the index and works in the same file of the class, where is the error?
Thanks in advantage to all.
:)

 

abidshahzad4u

5+ Year Member



 
Msg#: 3931134 posted 3:04 pm on Jun 11, 2009 (gmt 0)

$obj = new first();
$obj->get_last_five();

last77

5+ Year Member



 
Msg#: 3931134 posted 3:10 pm on Jun 11, 2009 (gmt 0)

Hi, thanks, but it still doesn't works.
:P

nick279

5+ Year Member



 
Msg#: 3931134 posted 5:00 pm on Jun 12, 2009 (gmt 0)

$obj = new first();
$obj->get_last_five();

This should work fine

Try removing the

include_once('config.php');

And make sure your class file works first

You don't need a blank constructor

nick279

5+ Year Member



 
Msg#: 3931134 posted 6:35 pm on Jun 12, 2009 (gmt 0)

Also

Keep your objects and wiring seperate

Objects =

bottle.php
can.php
paper.php
card.php

Wiring =

recycler.php
- get_can($id)
- get_paper($id)
- get_objects(1,100)

last77

5+ Year Member



 
Msg#: 3931134 posted 6:40 pm on Jun 12, 2009 (gmt 0)

Thanks!
I will try..
:)

nick279

5+ Year Member



 
Msg#: 3931134 posted 6:45 pm on Jun 12, 2009 (gmt 0)

Didn't mean to sound condescending / yell at you :) As the thread title suggests "starting with OOP in php" - it's very easy to get lost / can be very complex.

My #1 tip (as per last post) is to build objects which basically have a constructor which sets the values - and that's it. An object should be an object, nothing complex, no difficult methods, complex constructors etc. You should wire objects together with wiring (factories, builders) in another file altogether.

Best way to learn - google 'clean code talks' and watch the polymorphism video "The Clean Code Talks -- Inheritance, Polymorphism, & Testing"

last77

5+ Year Member



 
Msg#: 3931134 posted 7:00 pm on Jun 12, 2009 (gmt 0)

I understand what you want to say, i'm a beginner and every suggest maybe important for me.
:)
The problem is that I create a simple class with a echo and it doesn't work, but if I call the class from the original file of the class it works, I cant understando where is the error.
I'm driving crazy and I'm only at the start of my studies!
:P
So the class file is included in the index, if I create an instance of the class in is own file works, if I create in index not works, what can be the error^ maybe some settings of php.ini?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved