gaza165 Posted May 14, 2009 Share Posted May 14, 2009 I have a function inside my Class called RetrieveBlog(), this retrieves the blog from the database what i want to do is be able to call the method without making a new instance ofthe class Blog. class.php <?php session_start(); class Blog { function __construct($blogtitle,$blogbody,$blogcreated,$createdby,$blogimg,$shortdescription) { $this->blogtitle = $blogtitle; $this->blogbody = $blogbody; $this->blogcreated = $blogcreated; $this->createdby = $createdby; $this->blogimg = $blogimg; $this->shortdescription = $shortdescription; } function CheckInput() { if(empty($this->blogtitle)) { header("Location: index.php"); $_SESSION['msg'] = "Please enter the title"; } else if(empty($this->blogbody)) { header("Location: index.php"); $_SESSION['msg'] = "Please enter the body"; } else if(empty($this->createdby)) { header("Location: index.php"); $_SESSION['msg'] = "Please enter your name"; } else if(empty($this->blogimg)) { header("Location: index.php"); $_SESSION['msg'] = "Please enter the Blog Image url"; } else if(empty($this->shortdescription)) { header("Location: index.php"); $_SESSION['msg'] = "Please enter the Short Description"; } else { $this->InsertBlog(); } } function InsertBlog() { $title = $this->blogtitle; $body = $this->blogbody; $created = $this->blogcreated; $createdby = $this->createdby; $blogimg = $this->blogimg; $shortdescription = $this->shortdescription; $sql = mysql_query("INSERT INTO blog(title,body,name,blog_img,short_description) VALUES ('$title','$body','$createdby','$blogimg','$shortdescription')"); $_SESSION['msg'] = 'Blog Created'; header("Location: index.php"); } function retrieveBlog() { $sql = mysql_query("SELECT * FROM blog"); while($row = mysql_fetch_array($sql)) { echo $row['title']; } } } ?> getBlog.php <? include("class_lib.php"); include("dbconnect.php"); $blog = new Blog(); $blog->retrieveBlog(); ?> Because im making a new instance, its going to the constructr... all i want to do is call on a method inside my class from a seperate document. Link to comment https://forums.phpfreaks.com/topic/158119-need-help-with-oop-programming/ Share on other sites More sharing options...
premiso Posted May 14, 2009 Share Posted May 14, 2009 If you are using PHP 5, make the function static. Without instantiating the class, however, you will not be able to use the $this conext with that method. Link to comment https://forums.phpfreaks.com/topic/158119-need-help-with-oop-programming/#findComment-834066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.